class Inferencer: def __init__(self, path, small): self.net = Net() self.path = path self.small = small self.net.load_state_dict(torch.load(path)) def change_format(self, board): np_onehot = self.to_one_hot(board) tensor_onthot = torch.from_numpy(np_onehot).type(torch.float32) return tensor_onthot def inference(self, board): with torch.no_grad(): tensor = self.change_format(board) output = self.net(tensor) _, predicted = torch.max(output.data, 1) return self.transform(predicted.item()) def transform(self, value): if (self.small): return value * 3 else: return value * 30 def to_one_hot(self, board): onehot = np.zeros((1, NUM_OF_COLOR + 1, ROW_DIM, COLUMN_DIM)) for row in range(ROW_DIM): for col in range(COLUMN_DIM): color = board[row][col] onehot[0, color, row, col] = 1 return onehot
red_stride=rStride, nonred_stride=nStride, red_out=rOut, nonred_out=nOut, d="cuda:0" if enableCuda else "cpu") model.double() if enableCuda: model.cuda() # declare optimizer and gradient and loss function optimizer = optim.Adadelta(model.parameters(), lr=lr_rate) loss = torch.nn.MSELoss(reduction='mean') print("Loading model") model.load_state_dict(torch.load(modelFile)) print("Starting Testing") out_nrgs, test_val = model.test(images, energies, loss) print("Testing Finished") out_nrgs = convertToNumpy(out_nrgs, enableCuda) # scaling energies to mHa energies = 1000 * energies out_nrgs = 1000 * out_nrgs # calculating median absolute error for energies in range (100-400 mHa) abs_err = abs(np.sort(energies) - np.sort(out_nrgs)) median_err = np.median(abs_err) with open(outdir + "median_abs_error.txt", "w+") as f:
def get_chars(path_name): gcs = storage.Client() bucket = gcs.get_bucket('mail-scanner-bucket') blob = bucket.blob(path_name) blob.download_to_filename('image.jpg') image = cv2.imread('image.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) edged = cv2.Canny(gray, 100, 200) cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5] for c in cnts: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * peri, True) if len(approx) == 4: screenCnt = approx break cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2) warped = four_point_transform( image, screenCnt.reshape(4, 2) * (image.shape[0] / 480.0)) warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) warped = cv2.adaptiveThreshold(warped, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 17, 4) image = imutils.resize(warped, width=500) # Remove Salt and pepper noise saltpep = cv2.fastNlMeansDenoising(image, None, 9, 13) # blur blured = cv2.blur(saltpep, (3, 3)) # binary ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV) # dilation kernel = np.ones((5, 100), np.uint8) img_dilation = cv2.dilate(thresh, kernel, iterations=1) # find contours im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # sort contours sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[1]) matrix = [[], [], []] for i, ctr in enumerate(sorted_ctrs): # Get bounding box x, y, w, h = cv2.boundingRect(ctr) # Getting ROI roi = image[y:y + h, x:x + w] im = cv2.resize(roi, None, fx=4, fy=4, interpolation=cv2.INTER_CUBIC) ret_1, thresh_1 = cv2.threshold(im, 127, 255, cv2.THRESH_BINARY_INV) im, ctrs_1, hier = cv2.findContours(thresh_1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # sort contours sorted_ctrs_1 = sorted(ctrs_1, key=lambda ctr: cv2.boundingRect(ctr)[0]) count, p_height = 0, 0 for j, ctr_1 in enumerate(sorted_ctrs_1): # Get bounding box x_1, y_1, w_1, h_1 = cv2.boundingRect(ctr_1) if w_1 > 50 and h_1 > 50: # Getting ROI roi_1 = thresh_1[y_1:y_1 + h_1, x_1:x_1 + w_1] if j != 0: # print(h_1, p_height) if p_height < h_1: count = 0 matrix[count].append(roi_1) p_height = h_1 device = 'cuda' if torch.cuda.is_available() else 'cpu' model = Net() model.load_state_dict( torch.load('letter_model.pt', map_location=torch.device(device))) p = transforms.Compose([ transforms.Resize((28, 28)), transforms.ToTensor(), transforms.Normalize((0.5, ), (0.5, )) ]) real_pred = '' for img in matrix[0]: img = Image.fromarray(img) img = p(img) img = img.view(1, 28, 28) img = img.unsqueeze(0) img = model(img) prediction = list(img.cpu().detach().numpy()[0]) classes = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' max_pred = prediction.index(max(prediction)) real_pred += classes[max_pred] return real_pred
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2) # dataiter = iter(testloader) # images, labels = dataiter.next() # print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4))) net = Net() net.load_state_dict(torch.load(PATH)) # outputs = net(images) # _, predicted = torch.max(outputs, 1) # print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4))) correct = 0 total = 0 with torch.no_grad(): for data in testloader: images, labels = data outputs = net(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Accuracy of the network on the 10000 test images: %d %%' %
import torch import torch.onnx from cnn import Net #TODO Get this working # https://pytorch.org/tutorials/advanced/super_resolution_with_caffe2.html # Online web service that enhances photography. # A model class instance (class not shown) model = Net() model.train(False) # Load the weights from a file (.pth usually) state_dict = torch.load("model.pth") # Load the weights now into a model net architecture defined by our class print("loading model") model.load_state_dict(state_dict['model_state_dict']) # Input to the model x = torch.randn(10, 3, 224, 224, requires_grad=True) # Export the model torch_out = torch.onnx._export( model, # model being run x, # model input (or a tuple for multiple inputs) "model.onnx", # where to save the model (can be a file or file-like object) export_params=True ) # store the trained parameter weights inside the model file print("exported model")
def main(): parser = argparse.ArgumentParser(description='Prediction of TCR binding to peptide-MHC complexes') parser.add_argument('--infile', type=str, help='input file for training') parser.add_argument('--indepfile', type=str, default=None, help='independent test file') parser.add_argument('--blosum', type=str, default='data/BLOSUM50', help='file with BLOSUM matrix') parser.add_argument('--batch_size', type=int, default=50, metavar='N', help='batch size') parser.add_argument('--model_name', type=str, default='original.ckpt', help = 'if train is True, model name to be saved, otherwise model name to be loaded') parser.add_argument('--epoch', type = int, default=200, metavar='N', help='number of epoch to train') parser.add_argument('--lr', type=float, default=0.001, metavar='LR', help='learning rate') parser.add_argument('--cuda', type = str2bool, default=True, help = 'enable cuda') parser.add_argument('--seed', type=int, default=7405, help='random seed') parser.add_argument('--mode', default = 'train', type=str, help = 'train or test') parser.add_argument('--model', type=str, default='cnn', help='cnn, resnet') args = parser.parse_args() if args.mode is 'test': assert args.indepfile is not None, '--indepfile is missing!' ## cuda if torch.cuda.is_available() and not args.cuda: print("WARNING: You have a CUDA device, so you should probably run with --cuda") args.cuda = (args.cuda and torch.cuda.is_available()) device = torch.device('cuda' if args.cuda else 'cpu') ## set random seed seed = args.seed torch.manual_seed(seed) if args.cuda: torch.cuda.manual_seed(seed) if args.cuda else None # embedding matrix embedding = load_embedding(args.blosum) ## read data X_pep, X_tcr, y = data_io_tf.read_pTCR(args.infile) y = np.array(y) n_total = len(y) n_train = int(round(n_total * 0.8)) n_valid = int(round(n_total * 0.1)) n_test = n_total - n_train - n_valid idx_shuffled = np.arange(n_total); np.random.shuffle(idx_shuffled) idx_train, idx_valid, idx_test = idx_shuffled[:n_train], \ idx_shuffled[n_train:(n_train+n_valid)], \ idx_shuffled[(n_train+n_valid):] ## define dataloader train_loader = define_dataloader(X_pep[idx_train], X_tcr[idx_train], y[idx_train], None, None, None, batch_size=args.batch_size, device=device) valid_loader = define_dataloader(X_pep[idx_valid], X_tcr[idx_valid], y[idx_valid], None, maxlen_pep=train_loader['pep_length'], maxlen_tcr=train_loader['tcr_length'], batch_size=args.batch_size, device=device) test_loader = define_dataloader(X_pep[idx_test], X_tcr[idx_test], y[idx_test], None, maxlen_pep=train_loader['pep_length'], maxlen_tcr=train_loader['tcr_length'], batch_size=args.batch_size, device=device) ## read indep data if args.indepfile is not None: X_indep_pep, X_indep_tcr, y_indep = data_io_tf.read_pTCR(args.indepfile) y_indep = np.array(y_indep) indep_loader = define_dataloader(X_indep_pep, X_indep_tcr, y_indep, None, maxlen_pep=train_loader['pep_length'], maxlen_tcr=train_loader['tcr_length'], batch_size=args.batch_size, device=device) if args.model == 'cnn': from cnn import Net #if args.model == 'resnet': # # from resnet import Net # Net = models.resnet18 else: raise ValueError('unknown model name') ## define model model = Net(embedding, train_loader['pep_length'], train_loader['tcr_length']).to(device) optimizer = optim.Adam(model.parameters(), lr=args.lr) if 'models' not in os.listdir('.'): os.mkdir('models') if 'result' not in os.listdir('.'): os.mkdir('result') ## fit model if args.mode == 'train' : model_name = check_model_name(args.model_name) model_name = check_model_name(model_name, './models') model_name = args.model_name wf_open = open('result/'+os.path.splitext(os.path.basename(args.infile))[0]+'_'+os.path.splitext(os.path.basename(args.model_name))[0]+'_valid.csv', 'w') wf_colnames = ['loss', 'accuracy', 'precision1', 'precision0', 'recall1', 'recall0', 'f1macro','f1micro', 'auc'] wf = csv.DictWriter(wf_open, wf_colnames, delimiter='\t') t0 = time.time() for epoch in range(1, args.epoch + 1): train(args, model, device, train_loader['loader'], optimizer, epoch) ## evaluate performance perf_train = get_performance_batchiter(train_loader['loader'], model, device) perf_valid = get_performance_batchiter(valid_loader['loader'], model, device) ## print performance print('Epoch {} TimeSince {}\n'.format(epoch, timeSince(t0))) print('[TRAIN] {} ----------------'.format(epoch)) print_performance(perf_train) print('[VALID] {} ----------------'.format(epoch)) print_performance(perf_valid, writeif=True, wf=wf) ## evaluate and print test-set performance print('[TEST ] {} ----------------'.format(epoch)) perf_test = get_performance_batchiter(test_loader['loader'], model, device) print_performance(perf_test) model_name = './models/' + model_name torch.save(model.state_dict(), model_name) elif args.mode == 'test' : model_name = args.model_name assert model_name in os.listdir('./models') model_name = './models/' + model_name model.load_state_dict(torch.load(model_name)) ## evaluate and print independent-test-set performance print('[INDEP] {} ----------------') perf_indep = get_performance_batchiter(indep_loader['loader'], model, device) print_performance(perf_indep) ## write blackbox output wf_bb_open = open('data/testblackboxpred_' + os.path.basename(args.indepfile), 'w') wf_bb = csv.writer(wf_bb_open, delimiter='\t') write_blackbox_output_batchiter(indep_loader, model, wf_bb, device) wf_bb_open1 = open('data/testblackboxpredscore_' + os.path.basename(args.indepfile), 'w') wf_bb1 = csv.writer(wf_bb_open1, delimiter='\t') write_blackbox_output_batchiter(indep_loader, model, wf_bb1, device, ifscore=True) else : print('\nError: "--mode train" or "--mode test" expected')
if __name__ == '__main__': parser = argparse.ArgumentParser( description='Visualization tool for CNN Pytorch model') parser.add_argument('--use_cuda', action='store_true', default=False, help='enables CUDA training') parser.add_argument('--image', help='path to the image', required=True) parser.add_argument('--image_cls', help='cllass of the image', required=True) parser.add_argument('--load_weights', help='path to saved model\'s file', required=True) parser.add_argument( '--dataset', help='Path to dataset from which to load the images for visualizatiom') args = parser.parse_args() use_cuda = args.use_cuda and torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") model = Net().to(device) model.load_state_dict(torch.load(args.load_weights)) model.eval() image = np.array(Image.open(args.image)).astype(np.float32) / 255 image_tensor = torch.tensor(image).permute(2, 0, 1) plot_pixelwise_gradients(model, image_tensor, args.image_cls) plot_occlusion_heatmap(image_tensor, args.image_cls)
import torch.nn as nn import torch.nn.functional as F from torch.autograd import Variable import torchvision import torchvision.transforms as transforms import matplotlib.pyplot as plt import torch.optim as optim from cnn import Net bs = 16 model_name = 'cnn_adaptive_7.pt' #load model model = Net() model.load_state_dict(torch.load('models/' + model_name)) #data transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=False, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=bs, shuffle=False, num_workers=2)
def capture_solve(input_image): # Grayscale and adaptive threshold img = cv2.GaussianBlur(input_image, (5, 5), 0) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) mask = np.zeros(gray.shape, np.uint8) kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)) close = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, kernel1) div = np.float32(gray) / close isolate = np.uint8(cv2.normalize(div, div, 0, 255, cv2.NORM_MINMAX)) res2 = cv2.cvtColor(isolate, cv2.COLOR_GRAY2BGR) thresh = cv2.adaptiveThreshold(isolate, 255, 0, 1, 19, 2) contour, hier = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Grab the biggest contour (the board) max_area = 0 best_cnt = None for cnt in contour: area = cv2.contourArea(cnt) if area > 50000: if area > max_area: max_area = area best_cnt = cnt # Isolate board cv2.drawContours(mask, [best_cnt], 0, 255, -1) cv2.drawContours(mask, [best_cnt], 0, 0, 2) isolate = cv2.bitwise_and(isolate, mask) # === Use second order derivative filter to find the vertical and horizontal lines === # # Obtain vertical contours kernel_x = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 10)) dx = cv2.Sobel(isolate, cv2.CV_16S, 1, 0) dx = cv2.convertScaleAbs(dx) cv2.normalize(dx, dx, 0, 255, cv2.NORM_MINMAX) _, close = cv2.threshold(dx, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) close = cv2.morphologyEx(close, cv2.MORPH_DILATE, kernel_x, iterations=1) # Remove number contours, isolating vertical lines contour, _ = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for cnt in contour: x, y, w, h = cv2.boundingRect(cnt) if h / w > 5: cv2.drawContours(close, [cnt], 0, 255, -1) else: cv2.drawContours(close, [cnt], 0, 0, -1) close_x = cv2.morphologyEx(close, cv2.MORPH_CLOSE, None, iterations=2) # Obtain horizontal contours kernel_y = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 2)) dy = cv2.Sobel(isolate, cv2.CV_16S, 0, 2) dy = cv2.convertScaleAbs(dy) cv2.normalize(dy, dy, 0, 255, cv2.NORM_MINMAX) ret, close = cv2.threshold(dy, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) close = cv2.morphologyEx(close, cv2.MORPH_DILATE, kernel_y) # Remove number contours, isolating vertical lines contour, _ = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for cnt in contour: x, y, w, h = cv2.boundingRect(cnt) if w / h > 5: cv2.drawContours(close, [cnt], 0, 255, -1) else: cv2.drawContours(close, [cnt], 0, 0, -1) close_y = cv2.morphologyEx(close, cv2.MORPH_DILATE, None, iterations=2) # Get intersections isolate = cv2.bitwise_and(close_x, close_y) # Obtain centroids contour, _ = cv2.findContours(isolate, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) centroids = [] for cnt in contour: mom = cv2.moments(cnt) (x, y) = int(mom['m10'] / mom['m00']), int(mom['m01'] / mom['m00']) cv2.circle(img, (x, y), 4, (0, 255, 0), -1) centroids.append((x, y)) # Re-order centroids centroids = np.array(centroids, dtype=np.float32) c = centroids.reshape((100, 2)) c2 = c[np.argsort(c[:, 1])] b = np.vstack([c2[i * 10:(i + 1) * 10][np.argsort(c2[i * 10:(i + 1) * 10, 0])] for i in range(10)]) bm = b.reshape((10, 10, 2)) # Apply perspective transform and warp to 450x450 image = np.zeros((450, 450, 3), np.uint8) for i, j in enumerate(b): ri = i // 10 ci = i % 10 if ci != 9 and ri != 9: src = bm[ri:ri + 2, ci:ci + 2, :].reshape((4, 2)) dst = np.array([[ci * 50, ri * 50], [(ci + 1) * 50 - 1, ri * 50], [ci * 50, (ri + 1) * 50 - 1], [(ci + 1) * 50 - 1, (ri + 1) * 50 - 1]], np.float32) retval = cv2.getPerspectiveTransform(src, dst) warp = cv2.warpPerspective(res2, retval, (450, 450)) image[ ri * 50:(ri + 1) * 50 - 1, ci * 50:(ci + 1) * 50 - 1 ] = warp[ri * 50:(ri + 1) * 50 - 1, ci * 50:(ci + 1) * 50 - 1].copy() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 57, 5) # Filter out all numbers and noise to isolate boxes cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if len(cnts) == 2 else cnts[1] for c in cnts: area = cv2.contourArea(c) if area < 1000: cv2.drawContours(thresh, [c], -1, (0, 0, 0), -1) # Fix horizontal and vertical lines vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 5)) thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, vertical_kernel, iterations=9) horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 1)) thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, horizontal_kernel, iterations=4) invert = 255 - thresh cnts = cv2.findContours(invert, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if len(cnts) == 2 else cnts[1] cnts, _ = contours.sort_contours(cnts, method="top-to-bottom") sudoku_rows = [] row = [] for (i, c) in enumerate(cnts, 1): area = cv2.contourArea(c) if area < 50000: row.append(c) if i % 9 == 0: (cnts, _) = contours.sort_contours(row, method="left-to-right") sudoku_rows.append(cnts) row = [] squares = [] for row in sudoku_rows: for cc in row: x, y, w, h = cv2.boundingRect(cc) squares.append(image[y:y + h, x:x + w]) squares = np.reshape(squares, (-1, 9)) board = np.zeros((9, 9)) mean_list = [] for x in squares: for box in x: gray = cv2.cvtColor(box, cv2.COLOR_BGR2GRAY) img = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 57, 5) denoise = ndimage.median_filter(img, 5) white_pix_count = np.count_nonzero(denoise) mean_list.append(white_pix_count) else: mean = np.mean(mean_list) for col, x in enumerate(squares): for row, box in enumerate(x): gray = cv2.cvtColor(box, cv2.COLOR_BGR2GRAY) img = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 57, 5) denoise = ndimage.median_filter(img, 5) white_pix_count = np.count_nonzero(denoise) if white_pix_count > mean: device = 'cuda' if torch.cuda.is_available() else 'cpu' model = Net() model.load_state_dict(torch.load('model.pth', map_location=torch.device(device))) model.eval() p = transforms.Compose([transforms.Resize((28, 28)), transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]) img = Image.fromarray(denoise) img = p(img) img = img.view(1, 28, 28) img = img.unsqueeze(0) output = model(img) prediction = list(output.cpu()[0]) board[col][row] = prediction.index(max(prediction)) solve(board) return board