def main(): # device = torch.device("cuda") device = torch.device("cpu") cnn = CNN() cnn.eval() cnn.load_state_dict( torch.load('synthesizer_4.pkl', map_location={'cuda:0': 'cpu'})) cnn.to(device) generator = Generator() generator.load_state_dict( torch.load('7800.pkl', map_location={'cuda:0': 'cpu'})) generator.to(device) generator.eval() print("load cnn net.") #test_dataloader = my_dataset.get_test_train_data_loader() test_dataloader = my_dataset.get_test_data_loader() correct = 0 total = 0 for i, (images1, labels, _) in enumerate(test_dataloader): try: image = images1 image = Variable(image) image, labels = image.to(device), labels.to(device) # vimage = generator(image) predict_label = cnn(image) labels = labels.cpu() predict_label = predict_label.cpu() c0 = captcha_setting.ALL_CHAR_SET[np.argmax(predict_label[ 0, 0:captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c1 = captcha_setting.ALL_CHAR_SET[np.argmax( predict_label[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_label[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_label[0, 3 * captcha_setting.ALL_CHAR_SET_LEN:4 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] # c4 = captcha_setting.ALL_CHAR_SET[np.argmax(predict_label[0, 4 * captcha_setting.ALL_CHAR_SET_LEN:5 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] # c5 = captcha_setting.ALL_CHAR_SET[np.argmax(predict_label[0, 5 * captcha_setting.ALL_CHAR_SET_LEN:6 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] #c3 = captcha_setting.ALL_CHAR_SET[np.argmax(predict_label[0, 3 * captcha_setting.ALL_CHAR_SET_LEN:4 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] #predict_label = '%s%s%s%s' % (c0, c1, c2, c3) predict_label = '%s%s%s%s' % (c0, c1, c2, c3) true_label = one_hot_encoding.decode(labels.numpy()[0]) total += labels.size(0) #save_image(vimage,'temp_result/'+str(i)+'.png') # print(predict_label.upper(),'>>>>>',true_label) if (predict_label.upper() == true_label.upper()): correct += 1 if (total % 2000 == 0): print( 'Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total)) except: pass print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total))
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)