利用pytorch调用预训练好的模型到GPU上
modeldata = torch.load(‘resnet-34-kinetics.pth’)
利用pytorch调用预训练好的模型到CPU上
modeldata = torch.load(‘resnet-34-kinetics.pth’,map_location=lambda storage, loc: storage)
返回值modeldata是一个Dict形变量
import torch
#torch.load with map_location='cpu'
modeldata = torch.load('/Users/yunshanjinwen/Downloads/video-classification-3d-cnn-pytorch-master/resnet-34-kinetics.pth',map_location=lambda storage, loc: storage)
print(modeldata)
以下为部分结果截图:
{'epoch': 251, 'arch': 'resnet-34', 'state_dict': OrderedDict([('module.conv1.weight', tensor([[[[[-1.6517e-04, -1.9815e-03, 6.5790e-05, ..., -1.7464e-03,
7.1315e-04, 2.7513e-03],
[ 4.5178e-04, -2.0219e-03, 2.4164e-03, ..., -2.5019e-03,
-1.2443e-03, 5.2268e-03],
[-2.1355e-05, -4.8858e-03, 1.7820e-03, ..., -2.1954e-03,
-6.8539e-03, 3.0589e-03],
...,
[ 1.9253e-03, -8.9324e-03, -5.0934e-03, ..., 1.0542e-02,
-1.5762e-02, -1.0906e-02],
[ 5.8645e-03, -5.7808e-03, -6.5108e-03, ..., 1.3623e-02,
-1.0710e-02, -1.0448e-02],
[ 5.6550e-03, -2.1331e-03, -5.4692e-03, ..., 9.5111e-03,
-9.2105e-03, -1.0438e-02]],
[[-1.3501e-03, -1.9448e-03, 5.9943e-04, ..., -9.1701e-03,
-5.9438e-03, 1.9499e-03],
[ 6.4719e-04, 1.1618e-03, 9.6282e-03, ..., -1.2365e-02,
-1.3114e-02, 1.2741e-03],
[-2.8409e-03, -2.0466e-03, 1.5195e-02, ..., -8.6988e-03,
-2.0623e-02, -2.6702e-03],
...,
[-6.5846e-03, -1.2465e-02, 9.3114e-03, ..., 1.8109e-02,
-3.0516e-02, -2.4903e-02],
[-2.7478e-03, -1.2591e-02, 7.6693e-04, ..., 2.6830e-02,
-1.7754e-02, -2.1244e-02],
[-1.1187e-03, -7.1811e-03, 8.5726e-04, ..., 2.4925e-02,
-9.5690e-03, -1.7701e-02]],
[[-3.7975e-04, -1.7536e-03, 7.3226e-04, ..., -8.5807e-04,
-6.5082e-03, -1.6441e-03],
[ 1.7633e-04, -1.4810e-03, 2.2259e-03, ..., -2.3171e-03,
-6.1884e-03, 1.8897e-03],
[-1.8017e-03, -4.1376e-03, 3.9245e-03, ..., -9.8589e-04,
-7.4184e-03, 2.7875e-03],
...,
[-4.2223e-03, -1.0657e-02, -3.5849e-03, ..., 6.0524e-03,
-1.1682e-02, -2.2864e-03],
[ 1.4787e-03, -6.3200e-03, -4.7488e-03, ..., 9.9818e-03,
-6.3154e-03, -2.6875e-03],
[ 2.6545e-03, -3.0796e-03, -3.8646e-03, ..., 7.4478e-03,
-3.7495e-03, -2.2428e-03]],
...,
[[-2.1119e-03, -4.5376e-03, -3.5092e-03, ..., 6.7190e-03,
-3.1279e-03, 8.5857e-04],
[ 1.0150e-03, 3.3156e-04, -5.7779e-03, ..., 1.1072e-02,
5.0206e-03, 1.4566e-03],
[ 2.4621e-03, 4.6832e-03, -5.9863e-03, ..., 1.2540e-02,
1.3816e-02, 1.5569e-03],
...,
[ 1.4451e-03, 1.3931e-02, -4.5525e-03, ..., -7.6014e-03,
2.6069e-02, 1.6237e-02],
[ 3.3758e-03, 1.4173e-02, 3.9455e-03, ..., -1.5280e-02,
1.8754e-02, 1.8436e-02],
[ 1.9167e-03, 4.4112e-03, -2.0688e-03, ..., -1.7529e-02,
1.0156e-02, 1.9978e-02]],
[[-9.3798e-04, -2.9649e-03, -1.0596e-03, ..., 5.9191e-03,
-4.1590e-03, -4.0802e-04],
[ 1.0737e-03, 1.8740e-03, -1.1678e-03, ..., 7.6876e-03,
3.6807e-04, -1.4017e-03],
[-3.8353e-04, 4.8607e-03, 1.5030e-03, ..., 7.1256e-03,
5.4487e-03, -2.2527e-03],
...,
[-3.7074e-03, 7.9841e-03, 3.2824e-03, ..., -7.1977e-03,
1.4904e-02, 6.6936e-03],
[-4.5734e-04, 9.3516e-03, 8.8564e-03, ..., -1.3316e-02,
8.7967e-03, 9.6345e-03],
[-1.0142e-03, 1.8892e-03, 1.8647e-03, ..., -1.5309e-02,
2.1293e-03, 1.1659e-02]],
[[ 1.2707e-03, -2.5364e-03, -2.5039e-03, ..., 2.7059e-03,
-3.3206e-03, -3.1734e-04],
[ 2.1841e-03, 4.8987e-04, -3.3567e-03, ..., 3.8903e-03,
-1.4775e-05, -1.3130e-03],
[ 1.6373e-03, 2.4530e-03, -5.6659e-04, ..., 3.1308e-03,
3.0472e-03, -7.4361e-04],
...,
[ 9.8145e-04, 4.4584e-03, 3.9257e-04, ..., -5.2606e-03,
8.5127e-03, 4.5759e-03],
[ 3.1419e-03, 6.0384e-03, 3.2480e-03, ..., -7.9108e-03,
5.9465e-03, 6.3306e-03],
[ 3.4196e-03, 3.9352e-03, 8.4626e-05, ..., -7.8478e-03,
2.6639e-03, 9.3354e-03]]],
[[[ 3.1826e-03, -3.9083e-04, 2.7631e-03, ..., 1.2445e-03,
3.5504e-03, 5.7540e-03],
[ 1.6508e-03, -2.7875e-03, 2.9993e-03, ..., -1.3650e-03,
-4.9088e-04, 6.0505e-03],
[ 7.3249e-04, -6.2978e-03, 1.7591e-03, ..., -1.5505e-03,
-6.7559e-03, 3.2010e-03],
...,
[ 1.5534e-03, -1.1302e-02, -5.7629e-03, ..., 1.1669e-02,
-1.5454e-02, -1.0657e-02],
[ 5.7586e-03, -7.9878e-03, -7.2319e-03, ..., 1.5403e-02,
-9.7069e-03, -9.5870e-03],
[ 6.3916e-03, -2.9506e-03, -5.2517e-03, ..., 1.2734e-02,
-6.4773e-03, -7.9462e-03]],
[[ 1.4367e-03, -1.4081e-03, 2.5048e-03, ..., -6.8429e-03,
-4.2194e-03, 3.6865e-03],
[ 1.3551e-03, -3.8019e-04, 9.7055e-03, ..., -1.1571e-02,
-1.3097e-02, 1.3563e-03],
[-2.8726e-03, -4.4115e-03, 1.4667e-02, ..., -8.2142e-03,
-2.0923e-02, -2.9261e-03],
...,
[-7.7048e-03, -1.5822e-02, 7.8851e-03, ..., 2.0090e-02,
-2.9474e-02, -2.3885e-02],
[-4.0614e-03, -1.6593e-02, -1.7070e-03, ..., 2.8669e-02,
-1.6262e-02, -1.9752e-02],
[-1.7024e-03, -1.0067e-02, -7.3902e-04, ..., 2.8454e-02,
-5.6443e-03, -1.3667e-02]],
[[-3.7306e-04, -1.8846e-03, 2.8093e-03, ..., 3.5831e-03,
-3.8483e-03, -5.2592e-05],
[-1.9617e-03, -3.7479e-03, 2.8395e-03, ..., 1.2882e-03,
-4.8584e-03, 2.0222e-03],
[-4.2665e-03, -6.6585e-03, 4.4659e-03, ..., 2.7102e-03,
-6.3268e-03, 2.2944e-03],
...,
[-7.1471e-03, -1.3792e-02, -3.2862e-03, ..., 1.1423e-02,
-8.4678e-03, -7.9637e-04],
[-2.0912e-03, -1.0089e-02, -5.1965e-03, ..., 1.5624e-02,
-2.5270e-03, -5.0833e-04],
[-3.3325e-04, -5.9383e-03, -3.4250e-03, ..., 1.4735e-02,
2.3980e-03, 2.5883e-03]],
...,
[[-3.9780e-03, -5.7622e-03, -3.2335e-03, ..., 9.3236e-03,
-2.9158e-03, -6.6608e-04],
[-1.6278e-03, -1.5129e-03, -6.0462e-03, ..., 1.3632e-02,
4.7860e-03, -1.3097e-03],
[-5.8951e-04, 2.5761e-03, -6.4252e-03, ..., 1.5185e-02,
1.3295e-02, -1.7902e-03],
...,
[-1.6986e-03, 1.1773e-02, -4.8483e-03, ..., -3.9480e-03,
2.7079e-02, 1.4488e-02],
[ 1.4077e-04, 1.1783e-02, 3.8759e-03, ..., -1.1612e-02,
1.9443e-02, 1.6956e-02],
[-3.6040e-04, 3.0026e-03, -1.0595e-03, ..., -1.2545e-02,
1.2285e-02, 2.0569e-02]],
[[-5.5658e-04, -2.9366e-03, -3.2694e-04, ..., 8.3765e-03,
-4.3031e-03, -1.9468e-03],
[ 4.8606e-04, 1.3105e-03, -7.2353e-04, ..., 1.0209e-02,
-2.4490e-04, -3.8851e-03],
[-1.0648e-03, 4.5476e-03, 2.1382e-03, ..., 1.0001e-02,
4.7191e-03, -5.5219e-03],
...,
[-4.4670e-03, 7.8533e-03, 4.4083e-03, ..., -3.7440e-03,
1.5516e-02, 5.0958e-03],
[-1.9839e-03, 8.3202e-03, 9.2135e-03, ..., -9.9288e-03,
9.1018e-03, 8.1252e-03],
[-1.7988e-03, 1.5914e-03, 3.4165e-03, ..., -1.0172e-02,
4.0007e-03, 1.1853e-02]],
[[ 2.2157e-03, -1.8570e-03, -1.2651e-03, ..., 4.3907e-03,
-2.8428e-03, -1.8231e-04],
[ 2.6736e-03, 6.9189e-04, -2.3035e-03, ..., 6.1820e-03,
2.1933e-04, -1.8808e-03],
[ 1.0704e-03, 1.9680e-03, 5.9076e-05, ..., 5.7425e-03,
3.2826e-03, -1.8306e-03],
...,
[-1.0191e-04, 4.1664e-03, 1.7063e-03, ..., -1.1053e-03,
1.0151e-02, 4.5358e-03],
[ 1.0804e-03, 4.6322e-03, 3.6979e-03, ..., -4.3514e-03,
6.9915e-03, 6.0594e-03],
[ 1.4985e-03, 2.6453e-03, 1.1785e-03, ..., -2.8380e-03,
5.1495e-03, 1.1083e-02]]],
[[[ 3.3948e-03, -8.7736e-04, 1.8137e-03, ..., -1.0086e-03,
7.5722e-04, 2.0417e-03],
[ 1.9136e-03, -2.9493e-03, 2.0643e-03, ..., -3.4087e-03,
-3.0196e-03, 2.4724e-03],
[ 1.0296e-03, -5.9133e-03, 1.4791e-03, ..., -2.9957e-03,
-8.5194e-03, 6.5823e-04],
...,
[ 1.0295e-03, -1.0387e-02, -5.2850e-03, ..., 1.0404e-02,
-1.6091e-02, -1.2167e-02],
[ 4.4823e-03, -7.7576e-03, -6.7233e-03, ..., 1.4717e-02,
-1.0047e-02, -1.1117e-02],
[ 4.0894e-03, -4.2123e-03, -5.7149e-03, ..., 1.2092e-02,
-7.2291e-03, -9.7859e-03]],
[[ 3.5306e-03, -7.9120e-04, 2.3066e-03, ..., -7.5178e-03,
-5.6043e-03, 9.6763e-04],
[ 3.0809e-03, -1.4291e-04, 8.9053e-03, ..., -1.2567e-02,
-1.4507e-02, -1.4465e-03],
[-1.5262e-03, -4.2275e-03, 1.3679e-02, ..., -9.5503e-03,
-2.2142e-02, -5.1696e-03],
...,
[-7.1479e-03, -1.5229e-02, 7.2308e-03, ..., 1.7692e-02,
-3.0719e-02, -2.6090e-02],
[-4.4176e-03, -1.6538e-02, -2.2000e-03, ..., 2.6490e-02,
-1.7703e-02, -2.2279e-02],
[-3.5072e-03, -1.1650e-02, -2.3070e-03, ..., 2.5546e-02,
-8.2147e-03, -1.7359e-02]],
[[ 2.0415e-03, 1.8574e-04, 3.9080e-03, ..., 2.6968e-03,
-4.9285e-03, -1.9912e-03],
[ 4.1545e-04, -1.6426e-03, 3.9856e-03, ..., 5.3239e-04,
-5.8519e-03, 1.7739e-04],
[-2.4679e-03, -4.9617e-03, 5.2222e-03, ..., 1.2718e-03,
-7.0418e-03, 1.3447e-03],
...,
[-7.8384e-03, -1.3835e-02, -4.3135e-03, ..., 7.4099e-03,
-1.1336e-02, -3.5257e-03],
[-3.5307e-03, -1.0869e-02, -6.6153e-03, ..., 1.1457e-02,
-6.0638e-03, -4.3835e-03],
[-3.6411e-03, -8.9741e-03, -6.4577e-03, ..., 9.9622e-03,
-2.4232e-03, -2.8239e-03]],
...,
说明.state_dict()只是把所有模型的参数都以OrderedDict的形式存下来
利用下面的代码,得知有哪些参数:
for key, v in enumerate(modeldata):
print (key, v)
0 epoch
1 arch
2 state_dict
3 optimizer
需要知道参数的值,利用:
for key, v in modeldata.items():
print (key, v)
以下为部分结果截图:
epoch 251
arch resnet-34
state_dict OrderedDict([('module.conv1.weight', tensor([[[[[-1.6517e-04, -1.9815e-03, 6.5790e-05, ..., -1.7464e-03,
7.1315e-04, 2.7513e-03],
[ 4.5178e-04, -2.0219e-03, 2.4164e-03, ..., -2.5019e-03,
-1.2443e-03, 5.2268e-03],
[-2.1355e-05, -4.8858e-03, 1.7820e-03, ..., -2.1954e-03,
-6.8539e-03, 3.0589e-03],
...,
[ 1.9253e-03, -8.9324e-03, -5.0934e-03, ..., 1.0542e-02,
-1.5762e-02, -1.0906e-02],
[ 5.8645e-03, -5.7808e-03, -6.5108e-03, ..., 1.3623e-02,
-1.0710e-02, -1.0448e-02],
[ 5.6550e-03, -2.1331e-03, -5.4692e-03, ..., 9.5111e-03,
-9.2105e-03, -1.0438e-02]],
[[-1.3501e-03, -1.9448e-03, 5.9943e-04, ..., -9.1701e-03,
-5.9438e-03, 1.9499e-03],
[ 6.4719e-04, 1.1618e-03, 9.6282e-03, ..., -1.2365e-02,
-1.3114e-02, 1.2741e-03],
[-2.8409e-03, -2.0466e-03, 1.5195e-02, ..., -8.6988e-03,
-2.0623e-02, -2.6702e-03],
...,
[-6.5846e-03, -1.2465e-02, 9.3114e-03, ..., 1.8109e-02,
-3.0516e-02, -2.4903e-02],
[-2.7478e-03, -1.2591e-02, 7.6693e-04, ..., 2.6830e-02,
-1.7754e-02, -2.1244e-02],
[-1.1187e-03, -7.1811e-03, 8.5726e-04, ..., 2.4925e-02,
-9.5690e-03, -1.7701e-02]],
[[-3.7975e-04, -1.7536e-03, 7.3226e-04, ..., -8.5807e-04,
-6.5082e-03, -1.6441e-03],
[ 1.7633e-04, -1.4810e-03, 2.2259e-03, ..., -2.3171e-03,
-6.1884e-03, 1.8897e-03],
[-1.8017e-03, -4.1376e-03, 3.9245e-03, ..., -9.8589e-04,
-7.4184e-03, 2.7875e-03],
...,
[-4.2223e-03, -1.0657e-02, -3.5849e-03, ..., 6.0524e-03,
-1.1682e-02, -2.2864e-03],
[ 1.4787e-03, -6.3200e-03, -4.7488e-03, ..., 9.9818e-03,
-6.3154e-03, -2.6875e-03],
[ 2.6545e-03, -3.0796e-03, -3.8646e-03, ..., 7.4478e-03,
-3.7495e-03, -2.2428e-03]],
...,
[[-2.1119e-03, -4.5376e-03, -3.5092e-03, ..., 6.7190e-03,
-3.1279e-03, 8.5857e-04],
[ 1.0150e-03, 3.3156e-04, -5.7779e-03, ..., 1.1072e-02,
5.0206e-03, 1.4566e-03],
[ 2.4621e-03, 4.6832e-03, -5.9863e-03, ..., 1.2540e-02,
1.3816e-02, 1.5569e-03],
...,
[ 1.4451e-03, 1.3931e-02, -4.5525e-03, ..., -7.6014e-03,
2.6069e-02, 1.6237e-02],
[ 3.3758e-03, 1.4173e-02, 3.9455e-03, ..., -1.5280e-02,
1.8754e-02, 1.8436e-02],
[ 1.9167e-03, 4.4112e-03, -2.0688e-03, ..., -1.7529e-02,
1.0156e-02, 1.9978e-02]],
[[-9.3798e-04, -2.9649e-03, -1.0596e-03, ..., 5.9191e-03,
-4.1590e-03, -4.0802e-04],
[ 1.0737e-03, 1.8740e-03, -1.1678e-03, ..., 7.6876e-03,
3.6807e-04, -1.4017e-03],
[-3.8353e-04, 4.8607e-03, 1.5030e-03, ..., 7.1256e-03,
5.4487e-03, -2.2527e-03],
...,
[-3.7074e-03, 7.9841e-03, 3.2824e-03, ..., -7.1977e-03,
1.4904e-02, 6.6936e-03],
[-4.5734e-04, 9.3516e-03, 8.8564e-03, ..., -1.3316e-02,
8.7967e-03, 9.6345e-03],
[-1.0142e-03, 1.8892e-03, 1.8647e-03, ..., -1.5309e-02,
2.1293e-03, 1.1659e-02]],
[[ 1.2707e-03, -2.5364e-03, -2.5039e-03, ..., 2.7059e-03,
-3.3206e-03, -3.1734e-04],
[ 2.1841e-03, 4.8987e-04, -3.3567e-03, ..., 3.8903e-03,
-1.4775e-05, -1.3130e-03],
[ 1.6373e-03, 2.4530e-03, -5.6659e-04, ..., 3.1308e-03,
3.0472e-03, -7.4361e-04],
...,
[ 9.8145e-04, 4.4584e-03, 3.9257e-04, ..., -5.2606e-03,
8.5127e-03, 4.5759e-03],
[ 3.1419e-03, 6.0384e-03, 3.2480e-03, ..., -7.9108e-03,
5.9465e-03, 6.3306e-03],
[ 3.4196e-03, 3.9352e-03, 8.4626e-05, ..., -7.8478e-03,
2.6639e-03, 9.3354e-03]]],
[[[ 3.1826e-03, -3.9083e-04, 2.7631e-03, ..., 1.2445e-03,
3.5504e-03, 5.7540e-03],
[ 1.6508e-03, -2.7875e-03, 2.9993e-03, ..., -1.3650e-03,
-4.9088e-04, 6.0505e-03],
[ 7.3249e-04, -6.2978e-03, 1.7591e-03, ..., -1.5505e-03,
-6.7559e-03, 3.2010e-03],
...,
[ 1.5534e-03, -1.1302e-02, -5.7629e-03, ..., 1.1669e-02,
-1.5454e-02, -1.0657e-02],
[ 5.7586e-03, -7.9878e-03, -7.2319e-03, ..., 1.5403e-02,
-9.7069e-03, -9.5870e-03],
[ 6.3916e-03, -2.9506e-03, -5.2517e-03, ..., 1.2734e-02,
-6.4773e-03, -7.9462e-03]],
[[ 1.4367e-03, -1.4081e-03, 2.5048e-03, ..., -6.8429e-03,
-4.2194e-03, 3.6865e-03],
[ 1.3551e-03, -3.8019e-04, 9.7055e-03, ..., -1.1571e-02,
-1.3097e-02, 1.3563e-03],
[-2.8726e-03, -4.4115e-03, 1.4667e-02, ..., -8.2142e-03,
-2.0923e-02, -2.9261e-03],
...,
[-7.7048e-03, -1.5822e-02, 7.8851e-03, ..., 2.0090e-02,
-2.9474e-02, -2.3885e-02],
[-4.0614e-03, -1.6593e-02, -1.7070e-03, ..., 2.8669e-02,
-1.6262e-02, -1.9752e-02],
[-1.7024e-03, -1.0067e-02, -7.3902e-04, ..., 2.8454e-02,
-5.6443e-03, -1.3667e-02]],
[[-3.7306e-04, -1.8846e-03, 2.8093e-03, ..., 3.5831e-03,
-3.8483e-03, -5.2592e-05],
[-1.9617e-03, -3.7479e-03, 2.8395e-03, ..., 1.2882e-03,
-4.8584e-03, 2.0222e-03],
[-4.2665e-03, -6.6585e-03, 4.4659e-03, ..., 2.7102e-03,
-6.3268e-03, 2.2944e-03],
...,
[-7.1471e-03, -1.3792e-02, -3.2862e-03, ..., 1.1423e-02,
-8.4678e-03, -7.9637e-04],
[-2.0912e-03, -1.0089e-02, -5.1965e-03, ..., 1.5624e-02,
-2.5270e-03, -5.0833e-04],
[-3.3325e-04, -5.9383e-03, -3.4250e-03, ..., 1.4735e-02,
2.3980e-03, 2.5883e-03]],
...,
[[-3.9780e-03, -5.7622e-03, -3.2335e-03, ..., 9.3236e-03,
-2.9158e-03, -6.6608e-04],
[-1.6278e-03, -1.5129e-03, -6.0462e-03, ..., 1.3632e-02,
4.7860e-03, -1.3097e-03],
[-5.8951e-04, 2.5761e-03, -6.4252e-03, ..., 1.5185e-02,
1.3295e-02, -1.7902e-03],
...,
[-1.6986e-03, 1.1773e-02, -4.8483e-03, ..., -3.9480e-03,
2.7079e-02, 1.4488e-02],
[ 1.4077e-04, 1.1783e-02, 3.8759e-03, ..., -1.1612e-02,
1.9443e-02, 1.6956e-02],
[-3.6040e-04, 3.0026e-03, -1.0595e-03, ..., -1.2