implement a simple CNN model using pytorch
- 創建一個新環境
python3 -m venv env_name
- activate environment
source env_name/bin/activate
- 安裝requirement.txt中的套件
pip3 install -r requirements.txt
-
Using Medical Masks Dataset (https://www.kaggle.com/vtech6/medical-masks-dataset), comes from Eden Social Welfare Foundation which contains the pictures of people wearing medical masks along with the labels containing their descriptions
-
Download images and labels, transforming .xml to a .csv file, with header row 'filename', 'label', 'xmax', 'xmin', 'ymax', 'ymin'. For example:
filename label xmax xmin ymax ymin c1_1844849.jpg good 1246 127 1312 227 c1_1844849.jpg none 745 889 862 999 -
Split data for train and test, name the file as train.csv / test.csv.
- run preprocess.py for both train.csv and test.csv.
(remember to edit different output filename: croppedImg_xxxx / imgLabel_xxxx)
python3 preprocess.py
- 修改cnn.py中的config
# CONFIG
output_dir = 'AlexNet_is32_bs256_ep300_loss'
if not os.path.isdir('CNN_model/'+output_dir):
os.mkdir('CNN_model/'+output_dir)
logger = SummaryWriter('CNN_log/'+output_dir)
epochs = 500
bch_size = 256
lr = 0.001
imgSize = 32
save_freq = 50
istrain = True
modelPath = 'AlexNet_is32_bs256_ep300_loss+w/ep250.pkl'
- output_dir - model name.
- epochs - epoch number.
- bch_size - batch size.
- lr - learning rate.
- imgSize - input img size.
- save_freq - save modal every save_freq epochs
- istrain - if the model do train or test
- modelPath - model path for testing, work if istrain=False
- run cnn.py
python3 cnn.py
可以使用tensorboard觀察loss及accuracy變化
tensorboard --logdir CNN_log
- 修改istrain及modelPath
istrain = False
modelPath = 'AlexNet_is32_bs256_ep300_loss/ep250.pkl'
- run cnn.py, get classification result for each class.
python3 cnn.py
- 可以在main中更換不同optimizer
if __name__ == '__main__':
......
if istrain:
# create model & optimizer
cnn = CNN()
cnn.apply(initialize_parameters)
print(cnn)
# CHOOSE AN OPTIMIZER
optimizer = torch.optim.Adam(cnn.parameters(),lr=lr, betas=(0.9, 0.999), eps=1e-08)
#optimizer = torch.optim.SGD(cnn.parameters(),lr=lr)
#optimizer = torch.optim.SGD(cnn.parameters(),lr=lr,momentum=0.8)
#optimizer = torch.optim.RMSprop(cnn.parameters(),lr=lr,alpha=0.9)
......