def main(): cnn = CNN() cnn.load_state_dict(torch.load('model.pkl')) print("load cnn net.") test_dataloader = my_dataset.get_test_data_loader() correct = 0 total = 0 for i, (images, labels) in enumerate(test_dataloader): image = images vimage = Variable(image) predict_label = cnn(vimage) 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())] predict_label = '%s%s%s%s' % (c0, c1, c2, c3) true_label = one_hot_encoding.decode(labels.numpy()[0]) total += labels.size(0) if (predict_label == true_label): correct += 1 if (total % 200 == 0): print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total)) print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total))
def calulate_rate(): cnn = CNN() cnn.load_state_dict(torch.load('model.pkl')) print("load cnn net.") correct = 0 total = 0 for i, (images, labels) in enumerate(td): image = images vimage = Variable(image) predict_label = cnn(vimage) 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())] predict_label = '%s%s%s%s' % (c0, c1, c2, c3) true_label = one_hot_encoding.decode(labels.numpy()[0]) total += labels.size(0) if predict_label == true_label: correct += 1 correct_total = 100 * correct / total print(correct_total) return correct_total
def main(): cnn = CNN() cnn.load_state_dict(torch.load('model.pkl')) print("load cnn net.") predict_dataloader = my_dataset.get_predict_data_loader() vis = Visdom() for i, (images, labels) in enumerate(predict_dataloader): image = images vimage = Variable(image) predict_label = cnn(vimage) 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())] c = '%s%s%s%s' % (c0, c1, c2, c3) print(c) vis.images(image, opts=dict(caption=c))
def main(): cnn = CNN() 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() 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") 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(): if os.path.exists(directory): shutil.rmtree(directory) if not os.path.exists(directory): os.makedirs(directory) # device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") cnn = CNN() # cnn.to(device) cnn.eval() cnn.load_state_dict(torch.load('model.pkl')) print("Model Loaded") criterion = nn.MultiLabelSoftMarginLoss() data_loader = setting.get_test_data_loader() correct = 0 total = 0 count = 1 for i, (images, labels) in enumerate(data_loader): image = images variable = Variable(image) # variable = Variable(image).to(device) variable.requires_grad = True predict_label = cnn(variable) # predict_label = cnn(variable).to(device) loss = criterion(predict_label, labels.float()) cnn.zero_grad() loss.backward() data_grad = variable.grad.data perturbed_image = fgsm_attack(variable, epsilon, data_grad) predict_label = cnn(perturbed_image) numpy_image = perturbed_image.squeeze().detach().cpu().numpy() plt.imshow(numpy_image) fig = plt.gcf() fig.savefig(directory + '/' + str(count) + '.png') count += 1 c0 = setting.character_set[np.argmax(predict_label[0, 0:setting.character_set_length].data.numpy())] c1 = setting.character_set[np.argmax(predict_label[0, setting.character_set_length:2 * setting.character_set_length].data.numpy())] c2 = setting.character_set[np.argmax(predict_label[0, 2 * setting.character_set_length:3 * setting.character_set_length].data.numpy())] c3 = setting.character_set[np.argmax(predict_label[0, 3 * setting.character_set_length:4 * setting.character_set_length].data.numpy())] predict_label = '%s%s%s%s' % (c0, c1, c2, c3) true_label = setting.decode(labels.numpy()[0]) total += labels.size(0) if predict_label == true_label: correct += 1 if total % 20 == 0: print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total)) print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total))
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(): cnn = CNN() cnn.eval() parser = argparse.ArgumentParser(description="model path") parser.add_argument("--model-path", type=str) args = parser.parse_args() cnn.load_state_dict(torch.load(args.model_path)) predict_dataloader = datasets.get_predict_data_loader() #vis = Visdom() for i, (images, labels) in enumerate(predict_dataloader): image = images vimage = Variable(image) predict_label = cnn(vimage) 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())] c = '%s%s%s%s' % (c0, c1, c2, c3) print(c)
def main(): cnn = CNN() cnn.eval() cnn.load_state_dict(torch.load('model1.pkl')) print("load cnn net.") test_dataloader = my_dataset.get_test_data_loader() correct = 0 total = 0 #输入deepfool的最大迭代次数 max_iter = int(input("please input max_iter:")) for i, (images, labels) in enumerate(test_dataloader): images = torch.squeeze(images, dim=1) perturbed_data = Variable(deepfool(images, cnn, 248, 0, max_iter)) perturbed_label = cnn(perturbed_data) perturbed_label = perturbed_label.cpu() c0 = captcha_setting.ALL_CHAR_SET[np.argmax( perturbed_label[0, 0:captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c1 = captcha_setting.ALL_CHAR_SET[np.argmax( perturbed_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( perturbed_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( perturbed_label[0, 3 * captcha_setting.ALL_CHAR_SET_LEN:4 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] perturbed_label = '%s%s%s%s' % (c0, c1, c2, c3) true_label = one_hot_encoding.decode(labels.numpy()[0]) total += labels.size(0) if (perturbed_label == true_label): correct += 1 if (total % 10 == 0): print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total)) print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total))
def main(): cnn = CNN() cnn.eval() cnn.load_state_dict(torch.load('model.pkl')) print("load cnn net.") predict_dataloader = my_dataset.get_predict_data_loader() # vis = Visdom() results = [] for i, (images, labels) in enumerate(predict_dataloader): image = images vimage = Variable(image) predict_label = cnn(vimage) 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())] c = '%s%s%s%s' % (c0, c1, c2, c3) print(c) results.append(c) # vis.images(image, opts=dict(caption=c)) codes = get_file_codes() hits = get_result(codes, results) print float(len(hits)) / float(len(codes)) * 100
def predict(tensor_data): # 预测张量图像 try: cnn = CNN() cnn.eval() # 为什么会提升精度 cnn.load_state_dict(torch.load('model v2.5.pkl')) v_image = Variable(tensor_data) predict_label = cnn(v_image) c0 = ALL_CHAR_SET[np.argmax( predict_label[0, 0:ALL_CHAR_SET_LEN].data.numpy())] c1 = ALL_CHAR_SET[np.argmax( predict_label[0, ALL_CHAR_SET_LEN:2 * ALL_CHAR_SET_LEN].data.numpy())] c2 = ALL_CHAR_SET[np.argmax( predict_label[0, 2 * ALL_CHAR_SET_LEN:3 * ALL_CHAR_SET_LEN].data.numpy())] c3 = ALL_CHAR_SET[np.argmax( predict_label[0, 3 * ALL_CHAR_SET_LEN:4 * ALL_CHAR_SET_LEN].data.numpy())] # print(predict_label) c = '%s%s%s%s' % (c0, c1, c2, c3) # print(c) return c except: return "ERR"
def main(): cnn = CNN() cnn.eval() cnn.load_state_dict(torch.load('model.pkl')) predict_dataloader = my_dataset.get_predict_data_loader() for i, images in enumerate(predict_dataloader): v_image = Variable(images) print(type(v_image)) print(v_image.size()) predict_label = cnn(v_image) 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())] c = '%s%s%s%s' % (c0, c1, c2, c3) print(c)
def main(): cnn = CNN() if use_cuda: cnn = CNN().cuda() cnn.eval() cnn.load_state_dict(torch.load('model_splic_cos_300_512_0.001.pkl')) print("load cnn net.") predict_dataloader = my_dataset.get_predict_data_loader() #vis = Visdom() correct = 0 total = 0 for i, (images, labels) in enumerate(predict_dataloader): image = images vimage = Variable(image).cuda() predict_label = cnn(vimage).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())] # c = '%s%s%s%s' % (c0, c1, c2, c3) # print(c) predict_label = '%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_label, true_label) if (predict_label == true_label): correct += 1 if (total % 200 == 0): print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total)) print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total))
def main(): cnn = CNN() if use_cuda: cnn = CNN().cuda() cnn.eval() cnn.load_state_dict(torch.load('../model_91.pkl')) print("load cnn net.") predict_dataloader = my_dataset.get_label_data_loader() #vis = Visdom() correct = 0 total = 0 currentpath = os.getcwd() for i, (images, name) in enumerate(predict_dataloader): image = images vimage = Variable(image).cuda() predict_label = cnn(vimage).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())] predict_label = '%s%s%s%s' % (c0, c1, c2, c3) #true_label = one_hot_encoding.decode(labels.numpy()[0]) total += images.size(0) # print(predict_label, true_label) # if (predict_label == true_label): # correct += 1 os.chdir("./dataset/label/") #now = str(int(time.time())) print(name[0], predict_label) os.rename(str(name[0]), (str(predict_label) + '.png')) # 文件重新命名 os.chdir(currentpath) # 改回程序运行前的工作目录 sys.stdin.flush() # 刷新 os.chdir(currentpath) # 改回程序运行前的工作目录 sys.stdin.flush() # 刷新
def main(): cnn = CNN() cnn.eval() cnn.load_state_dict(torch.load('model1.pkl')) print("load cnn net.") test_dataloader = my_dataset.get_test_data_loader() correct = 0 total = 0 epsilon = float(input("please input epsilon = ")) iteration = int(input("please input iteration = ")) criterion = nn.MultiLabelSoftMarginLoss() alpha = 1 for i, (image, label) in enumerate(test_dataloader): vimage = Variable(image) labels = Variable(label.float()) perturbed_data = i_fgsm(vimage, image, labels, criterion, cnn, alpha, iteration, epsilon) perturbed_label = cnn(perturbed_data) perturbed_label = decode_to(perturbed_label) true_label = one_hot_encoding.decode(label.numpy()[0]) total += label.size(0) if (perturbed_label == true_label): correct += 1 if (total % 10 == 0): print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total)) print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total))
def main(): cnn = CNN() cnn.eval() cnn.load_state_dict(torch.load('model.pkl')) print("load cnn net.") test_dataloader = my_dataset.get_test_data_loader() correct = 0 total = 0 wrong_letter = [] total_letter = {} for i in captcha_setting.ALPHABET: total_letter[i] = { 'times': 0, 'success': 0, 'fail': 0 } for i in captcha_setting.NUMBER: total_letter[i] = { 'times': 0, 'success': 0, 'fail': 0 } for i, (images, labels) in enumerate(test_dataloader): image = images vimage = Variable(image) predict_label = cnn(vimage) c = captcha_setting.ALL_CHAR_SET[np.argmax(predict_label[0, 0:captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] predict_label = '%s' % (c) print('predict_label: ' + predict_label) true_label = one_hot_encoding.decode(labels.numpy()[0]) total += labels.size(0) print('true_label: ' + true_label) total_letter[true_label]['times'] += 1 if(predict_label == true_label): total_letter[true_label]['success'] += 1 correct += 1 else: total_letter[true_label]['fail'] += 1 wrong_letter.append(true_label) if(total%200==0): print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total)) print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total)) print(wrong_letter) for i in total_letter: print('%s, success: %d fail: %d' % (i, total_letter[i]['success'], total_letter[i]['fail']))
def cnn_recog(image_path): cnn = CNN() cnn.eval() cnn.load_state_dict(torch.load('./model.pkl')) print("load cnn net.") recog_dataloader = get_recog_data_loader(image_path) print(image_path) #vis = Visdom() for i, (images, labels) in enumerate(recog_dataloader): image = images vimage = Variable(image) predict_label = cnn(vimage) c = captcha_setting.ALL_CHAR_SET[np.argmax(predict_label[0, 0:captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c = '%s' % (c) return c
def main(): # device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") cnn = CNN() # cnn.to(device) cnn.eval() cnn.load_state_dict(torch.load('model.pkl')) print("Model Loaded") data_loader = setting.get_test_data_loader() correct = 0 total = 0 for i, (images, labels) in enumerate(data_loader): image = images variable = Variable(image) # variable = Variable(image).to(device) predict_label = cnn(variable) # predict_label = cnn(variable).to(device) c0 = setting.character_set[np.argmax( predict_label[0, 0:setting.character_set_length].data.numpy())] c1 = setting.character_set[np.argmax( predict_label[0, setting.character_set_length:2 * setting.character_set_length].data.numpy())] c2 = setting.character_set[np.argmax( predict_label[0, 2 * setting.character_set_length:3 * setting.character_set_length].data.numpy())] c3 = setting.character_set[np.argmax( predict_label[0, 3 * setting.character_set_length:4 * setting.character_set_length].data.numpy())] predict_label = '%s%s%s%s' % (c0, c1, c2, c3) true_label = setting.decode(labels.numpy()[0]) total += labels.size(0) if predict_label == true_label: correct += 1 if total % 200 == 0: print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total)) 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)
def main(): cnn = CNN() cnn.eval() cnn.load_state_dict(torch.load('model1.pkl')) print("load cnn net.") test_dataloader = my_dataset.get_test_data_loader() correct = 0 total = 0 epsilon = float(input("please input epsilon = ")) criterion = nn.MultiLabelSoftMarginLoss() for i, (images, labels) in enumerate(test_dataloader): vimage = Variable(images) labels = Variable(labels.float()) vimage.requires_grad = True predict_labels = cnn(vimage) loss = criterion(predict_labels, labels) cnn.zero_grad() loss.backward() data_grad = vimage.grad.data perturbed_data = fgsm_attack(vimage, epsilon, data_grad) perturbed_label = cnn(perturbed_data) c0 = captcha_setting.ALL_CHAR_SET[np.argmax( perturbed_label[0, 0:captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c1 = captcha_setting.ALL_CHAR_SET[np.argmax( perturbed_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( perturbed_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( perturbed_label[0, 3 * captcha_setting.ALL_CHAR_SET_LEN:4 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] perturbed_label = '%s%s%s%s' % (c0, c1, c2, c3) true_label = one_hot_encoding.decode(labels.numpy()[0]) total += labels.size(0) if (perturbed_label == true_label): correct += 1 if (total % 100 == 0): print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total)) print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total))
import PIL.ImageFile as ImageFile import numpy as np import torch import torchvision.transforms as transforms from PIL import Image from torch.autograd import Variable import captcha_setting from captcha_cnn_model import CNN transform = transforms.Compose([ transforms.Grayscale(), transforms.ToTensor(), ]) cnn = CNN() cnn.load_state_dict(torch.load('model.pkl')) def predict_image(image): """ 预测image :param image: :return: """ image = image.resize((captcha_setting.IMAGE_WIDTH, captcha_setting.IMAGE_HEIGHT)) image = transform(image) vimage = Variable(image[np.newaxis, :]) predict_label = cnn(vimage) char_set_len = captcha_setting.ALL_CHAR_SET_LEN c0 = captcha_setting.ALL_CHAR_SET[np.argmax(predict_label[0, 0:char_set_len].data.numpy())]
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")
def main(): device = torch.device("cuda") #if torch.cuda.is_available() else "cpu") # Train the Model print('loading data') train_dataloader = my_dataset.get_train_data_loader() test_dataloader = my_dataset.get_test_data_loader() print('start training') loss_list = [] accuracy_list = [] for ttest_num in range(0, 5): cnn = nn.DataParallel(CNN()) # cnn.load_state_dict(torch.load('./'+model_name)) # cnn.to(device) cnn.cuda() # generator = Generator() # generator.load_state_dict(torch.load('7800.pkl')) # generator.to(device) # generator.eval() cnn.train() print('init net') criterion = nn.MultiLabelSoftMarginLoss() optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate) print('>>>>>>>>>>>>Round:', ttest_num) accuracy = 0 for epoch in range(num_epochs): loss_total = 0 start = datetime.now() for i, (images1, labels, _) in enumerate(train_dataloader): #print(i) images1 = Variable(images1) labels = Variable(labels.float()) images1, labels = images1.to(device), labels.to(device) # images1 = generator(images1) predict_labels = cnn(images1) # print(predict_labels.type) # print(labels.type) loss = criterion(predict_labels, labels) optimizer.zero_grad() loss.backward() optimizer.step() loss_total += loss.item() # if (i+1) % 5000 == 0: # print("epoch:", epoch, "step:", i, "loss:", loss.item()) # if (i+1) % 25000 == 0: # print("save model") correct = 0 total = 0 # print(len(test_dataloader)) for i, (images1, labels, _) in enumerate(test_dataloader): # print('///////////////////////////////') # try: image = images1 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() 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())] c6 = captcha_setting.ALL_CHAR_SET[np.argmax(predict_label[ 0, 6 * captcha_setting.ALL_CHAR_SET_LEN:7 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c7 = captcha_setting.ALL_CHAR_SET[np.argmax(predict_label[ 0, 7 * captcha_setting.ALL_CHAR_SET_LEN:8 * 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%s%s%s%s' % (c0, c1, c2, c3, c4, c5, c6, c7) # predict_label = '%s%s%s%s' % (c0, c1, c2, c3) true_label = one_hot_encoding.decode(labels.numpy()[0]) total += 1 #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 # try: print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total)) # except: # pass loss_list.append(loss_total) with open("loss_record_6.txt", "wb") as fp: #Pickling pickle.dump(loss_list, fp) stop = datetime.now() # try: if (correct / total > accuracy): accuracy = correct / total # torch.save(cnn.state_dict(), "./model_lake/"+model_name.replace('.','_'+str(ttest_num)+'.')) #current is model.pickle torch.save(cnn.state_dict(), "./model_lake/" + model_name) #current is model.pickle print('saved!!!!!!!!!!!!!!!!!!!!!!!') # except: # pass print("epoch:", epoch, "step:", i, " time:<", stop - start, "> loss:", loss_total) accuracy_list.append(accuracy) print(sum(accuracy_list) / len(accuracy_list)) # torch.save(cnn.state_dict(), "./"+model_name) #current is model.pkl # print("save last model") print(accuracy_list)