def main(): cnn = CNN() cnn.train() print('init net') criterion = nn.MultiLabelSoftMarginLoss() optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate) # Train the Model train_dataloader = my_dataset.get_train_data_loader(batch_size=batch_size) for epoch in range(num_epochs): for i, (images, labels) in enumerate(train_dataloader): images = Variable(images) labels = Variable(labels.float()) predict_labels = cnn(images) # print(predict_labels.type) # print(labels.type) loss = criterion(predict_labels, labels) optimizer.zero_grad() loss.backward() optimizer.step() if (i + 1) % 10 == 0: print("epoch:", epoch, "step:", i, "loss:", loss.item()) if (i + 1) % 100 == 0: torch.save(cnn.state_dict(), "./model.pkl") #current is model.pkl print("save model") print("epoch:", epoch, "step:", i, "loss:", loss.item()) torch.save(cnn.state_dict(), "./model.pkl") #current is model.pkl print("save last model")
def main(): # device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") cnn = CNN() # cnn.to(device) cnn.train() print('Model Initialization') criterion = nn.MultiLabelSoftMarginLoss() optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate) # Train the Model train_data_loader = setting.get_train_data_loader() for epoch in range(num_epochs): for i, (images, labels) in enumerate(train_data_loader): images = Variable(images) # images = Variable(images).to(device) labels = Variable(labels.float()) # labels = Variable(labels.float()).to(device) predict_labels = cnn(images) loss = criterion(predict_labels, labels) optimizer.zero_grad() loss.backward() optimizer.step() if (i + 1) % 10 == 0: print("epoch:", epoch, "step:", i, "loss:", loss.item()) if (i + 1) % 100 == 0: torch.save(cnn.state_dict(), "./model.pkl") print("Model Saved") print("epoch:", epoch, "step:", i, "loss:", loss.item()) torch.save(cnn.state_dict(), "./model.pkl") print("Last Model Saved")
def main(): device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu") accuracy_list = [] # Train the Model print("Loading Data") train_dataloader = my_dataset.get_train_data_loader() test_dataloader = my_dataset.get_test_data_loader() print("Data Ready") accuracy = 0 for ttest_num in range(0, 5): print('>>>>>>>>>>>>>>>>> ROUND: ', ttest_num) cnn = CNN() cnn.to(device) cnn.train() print('init net') criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate) accuracy = 0 for epoch in range(num_epochs): print('....') for i, (images1, labels) in enumerate(train_dataloader): images1 = Variable(images1) labels = Variable(labels) #.view(-1,1)) images, labels = images1.to(device), labels.to(device) # images = generator(images) #labels = torch.tensor(labels, dtype=torch.long, device=device) predict_labels = cnn(images) #.view(1,-1)[0] # print(predict_labels.type) # print(labels.type) # print(images.shape) #print(labels) #print(predict_labels) loss = criterion(predict_labels, labels) optimizer.zero_grad() loss.backward() optimizer.step() # if (i+1) % 1500 == 0: # print("epoch:", epoch, "step:", i, "loss:", loss.item()) # if (i+1) % 2500 == 0: # print("save model") correct = 0 total = 0 for i, (images1, labels) in enumerate(test_dataloader): image = images1 #print(image) image = Variable(image) image, labels = image.to(device), labels.to(device) # image = generator(image) predict_label = cnn(image) labels = labels.cpu() predict_label = predict_label.cpu() _, predicted = torch.max(predict_label, 1) total += labels.size(0) # print(predicted,'>>>>>>>>',labels) if (predicted == labels): correct += 1 print( 'Test Accuracy of the model on the %d test images (%d): %f %%' % (total, correct, 100 * correct / total)) if (correct / total > accuracy): accuracy = correct / total torch.save( cnn.state_dict(), "./model_lake/" + model_name.replace('.', '_' + str(ttest_num) + '.')) # torch.save(cnn.state_dict(), "./model_lake/"+model_name) #current is model.pickle print('saved!!!!!!!!!!!!!!!!!!!!!!!') # torch.save(cnn.state_dict(), "./"+model_name) #current is model.pkl print("epoch:", epoch, "step:", i, "loss:", loss.item()) # print('final accuracy: ',accuracy) accuracy_list.append(accuracy) print(accuracy_list)
def main(): cnn = CNN() #resume resume = './model_cos_90_0.01.pkl' checkpoint = torch.load(resume) cnn.load_state_dict(checkpoint) if use_cuda: cnn = CNN().cuda() cnn.train() print('init net') criterion = nn.MultiLabelSoftMarginLoss() optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate) best_acc = 0 # Train the Model train_dataloader = my_dataset.get_finetune_data_loader() for epoch in range(num_epochs): correct = 0 total = 0 for i, (images, labels) in enumerate(train_dataloader): #lr_schedule lr = cosine_anneal_schedule(epoch) for param_group in optimizer.param_groups: #print(param_group['lr']) param_group['lr'] = lr images = Variable(images).cuda() labels = Variable(labels.float()).cuda() predict_labels = cnn(images) # print(predict_labels.type) # print(labels.type) loss = criterion(predict_labels, labels) optimizer.zero_grad() loss.backward() optimizer.step() predict_labels = predict_labels.cpu() labels = labels.cpu() c0 = captcha_setting.ALL_CHAR_SET[np.argmax(predict_labels[0, 0:captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c1 = captcha_setting.ALL_CHAR_SET[np.argmax(predict_labels[0, captcha_setting.ALL_CHAR_SET_LEN:2 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c2 = captcha_setting.ALL_CHAR_SET[np.argmax(predict_labels[0, 2 * captcha_setting.ALL_CHAR_SET_LEN:3 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c3 = captcha_setting.ALL_CHAR_SET[np.argmax(predict_labels[0, 3 * captcha_setting.ALL_CHAR_SET_LEN:4 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] # c = '%s%s%s%s' % (c0, c1, c2, c3) # print(c) predict_labels = '%s%s%s%s' % (c0, c1, c2, c3) # predict = '%s%s%s%s' % (c0, c1, c2, c3) # predict_label = one_hot_encoding.decode(predict.numpy()[0]) true_label = one_hot_encoding.decode(labels.numpy()[0]) total += labels.size(0) print(predict_labels, true_label) if (predict_labels == true_label): correct += 1 acc = 100 * correct / total # if (total % 200 == 0): # print('Test Accuracy of the model on the %d train images: %f %%' % (total, 100 * correct / total)) if (i+1) % 10 == 0: print("epoch:", epoch, "step:", i, "loss:", loss.item(),"Accuracy:", acc) if acc >= best_acc: torch.save(cnn.state_dict(), "./model_cos_90_0.01_finetune_2.pkl") #current is model.pkl print("save model") #print("epoch:", epoch, "loss:", loss.item(),"Accuracy:", acc) #torch.save(cnn.state_dict(), "./model_cos_90_0.01_finetune.pkl") #current is model.pkl print("save last model")