Don't worry, be happy.

2019-02-23
Parameter size of different network architecture in torchvision.models

There are some built in models in torchvision.models, such as vgg or resnet. The same page also record a table about the Top1 & Top5 errors of different architecture. However, the parameter size of different models is missing, so I write a program and complement the table.

Read More

2019-02-22
TIL Using Pickle to save sklearn model

The models in sklearn are objects, using Pickle to save and restore the trained classify or cluster models. Do not us numpy.save and numpy.load to do this, it’s not working.

An example:

1
2
3
4
5
6
7
8
9
10
11
12
13
import Pickle
from sklearn.cluster import Kmeans
# suppose we want to save this kmeans model
kmeans_results = KMeans(n_clusters=2).fit(X)
kmeans_results_file = "kmeans.pkl"

# saving results
with open(kmeans_results_file, 'wb') as f:
pickle.dump(kmeans_results, f)

# loading results
with open(kmeans_results_file, 'rb') as f:
kmeans_results = pickle.load(f)

Read More

2019-02-22
Load pytorch model trained with nn.DataParallel

I encountered a problem when loading pytorch model trained with nn.DataParallel, which gives some errors like follows:

1
2
Missing key(s) in state_dict: "embedding_net.conv1.weight" ...
Unexpected key(s) in state_dict: "module.embedding_net.conv1.weight" ...

It seems that pytorch automatically warp the model with module, after google it around I found this solution. Basically two workarounds:

  1. Remove module in the key manually

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # original saved file with DataParallel
    state_dict = torch.load('myfile.pth.tar')
    # create new OrderedDict that does not contain `module.`
    from collections import OrderedDict
    new_state_dict = OrderedDict()
    for k, v in state_dict.items():
    name = k[7:] # remove `module.`
    new_state_dict[name] = v
    # load params
    model.load_state_dict(new_state_dict)
  2. When save parameters, don’t save original model, save the parameter of module

    1
    2
    torch.save(model.module.state_dict(), path_to_file) # saving this
    torch.save(model.state_dict(), path_to_file) # instead of this
Read More

2019-01-30

Eigen真好用!

vscode比sublime text好用!还免费!

两个程序员之间最遥远的距离,莫过于他用大小写命名,而你用下划线。

他用

1
2
while (true) {
}

而你用

1
2
3
while (true) 
{
}

Read More

2019-01-21
TIL python variables names cannot begin with a digit

C++ variables also cannot start with number. It’s a compiler issue, because a variable starts with number can be both a valid number or a variable…

Read More