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
def train(train_files, test_files, train_batch_size, eval_batch_size, model_file, vocab_size, num_classes, n_epoch, print_every=50, eval_every=500): torch.multiprocessing.set_sharing_strategy('file_system') torch.backends.cudnn.benchmark = True print "Setting seed..." seed = 1234 torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) # setup CNN model CONFIG["vocab_size"] = vocab_size CONFIG["num_classes"] = num_classes model = Net() if torch.cuda.is_available(): print "CUDA is available on this machine. Moving model to GPU..." model.cuda() print model criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters()) train_set = HDF5Dataset(train_files) test_set = HDF5Dataset(test_files) train_loader = DataLoader(dataset=train_set, batch_size=train_batch_size, shuffle=True, num_workers=2) test_loader = DataLoader(dataset=test_set, batch_size=eval_batch_size, num_workers=2) _train_loop(train_loader=train_loader, test_loader=test_loader, model=model, criterion=criterion, optimizer=optimizer, n_epoch=n_epoch, print_every=print_every, eval_every=eval_every, model_file=model_file)
def setUp(self): dataset, classes = prepare_cifar10() self.models_path = "./models" self.trainer = SimpleTrainer(pruning=True, datasets=dataset, models_path=self.models_path) experiment = ax.Experiment( name="model_test", search_space=search_space(), ) sobol = ax.Models.SOBOL(search_space=experiment.search_space) self.param = sobol.gen(1).arms[0].parameters self.net = Net(self.param, classes=classes, input_shape=(3, 32, 32)) if not os.path.exists(self.models_path): os.mkdir(self.models_path)
class TrainingTestCase(unittest.TestCase): """ Test cases to see if we can build a model, train it, evaluate, quantize it and prune it""" def setUp(self): dataset, classes = prepare_cifar10() self.models_path = "./models" self.trainer = SimpleTrainer(pruning=True, datasets=dataset, models_path=self.models_path) experiment = ax.Experiment( name="model_test", search_space=search_space(), ) sobol = ax.Models.SOBOL(search_space=experiment.search_space) self.param = sobol.gen(1).arms[0].parameters self.net = Net(self.param, classes=classes, input_shape=(3, 32, 32)) if not os.path.exists(self.models_path): os.mkdir(self.models_path) def tearDown(self): shutil.rmtree(self.models_path) shutil.rmtree("./data") def test_training(self): self.trainer.load_dataloaders(batch_size=4, collate_fn=None) self.trainer.train(self.net, self.param, name="0", epochs=1, reload=False, old_net=None) self.trainer.evaluate(self.net.to('cpu'), quant_mode=False)
from cnn import Net import data_processing from data_processing import process_data import torch import torch.nn as nn import torch.optim as optim from tqdm import tqdm train, test, val = process_data() device = torch.device('cuda:0') net = Net().to(device) def train_model(net): X = torch.Tensor([i[0] for i in train]).view(-1, 98, 98).to(device) # Normalize data X = X / 255.0 y = torch.Tensor([i[1] for i in train]).to(device) optimizer = optim.Adam(net.parameters(), lr=0.001) loss_function = nn.MSELoss() epochs = 2 batch_size = 32 for epoch in tqdm(range(epochs)): running_loss = 0.0 for i in range(0, len(X), batch_size): batch_X = X[i: i+batch_size].view(-1, 1, 98, 98).to(device) batch_y = y[i: i+batch_size].to(device)
def main(): # load images as a numpy array train_dataset = np.array( np.load('/content/drive/My Drive/McGill/comp551/data/train_max_x', allow_pickle=True)) train_dataset = train_dataset / 255.0 train_dataset = train_dataset.astype('float32') targets = pd.read_csv( '/content/drive/My Drive/McGill/comp551/data/train_max_y.csv', delimiter=',', skipinitialspace=True) targets = targets.to_numpy() # remove id column targets = targets[:, 1] targets = targets.astype(int) X_train, X_test, y_train, y_test = train_test_split(train_dataset, targets, test_size=0.2, random_state=42) # Clean memory train_dataset = None # converting training images into torch format dim1, dim2, dim3 = X_train.shape X_train = X_train.reshape(dim1, 1, dim2, dim3) X_train = torch.from_numpy(X_train) y_train = torch.from_numpy(y_train) # converting validation images into torch format dim1, dim2, dim3 = X_test.shape X_test = X_test.reshape(dim1, 1, dim2, dim3) X_test = torch.from_numpy(X_test) y_test = torch.from_numpy(y_test) # defining the model model = Net() criterion = nn.NLLLoss() optimizer = optim.SGD(model.parameters(), lr=0.003, momentum=0.9) if torch.cuda.is_available(): model = model.cuda() criterion = criterion.cuda() print(model) time0 = time() epochs = 1 for e in range(epochs): model.train() running_loss = 0 x_train, y_train = Variable(X_train).cuda(), Variable(y_train).cuda() x_val, y_val = Variable(X_test).cuda(), Variable(y_test).cuda() # converting the data into GPU format # if torch.cuda.is_available(): # x_train = x_train.cuda() # y_train = y_train.cuda() # x_val = x_val.cuda() # y_val = y_val.cuda() # clearing the Gradients of the model parameters optimizer.zero_grad() # prediction for training and validation set output_train = model(x_train) output_val = model(x_val) # computing the training and validation loss loss_train = criterion(output_train, y_train) loss_val = criterion(output_val, y_val) # computing the updated weights of all the model parameters loss_train.backward() # And optimizes its weights here optimizer.step() running_loss += loss_train.item() print("Epoch {} - Training loss: {}".format( e, running_loss / len(train_dataset))) print("\nTraining Time (in minutes) =", (time() - time0) / 60) # prediction for validation set with torch.no_grad(): output = model(x_val.cuda()) ps = torch.exp(output).cpu() probab = list(ps.numpy()) predictions = np.argmax(probab, axis=1) # accuracy on validation set print("\nModel Accuracy =", (accuracy_score(y_val, predictions)))
np.shape(images)) # hyperparameters (start with r = for reducing layer, start with n = for nonreducing layer) lr_rate = float(param["learning_rate"]) rKern = int(param["reducing_conv_kernel"]) nKern = int(param["nonreducing_conv_kernel"]) rStride = int(param["reducing_stride"]) nStride = int(param["nonreducing_stride"]) rOut = int(param["reducing_out"]) nOut = int(param["nonreducing_out"]) # constructing a model (converting model to double precision) model = Net(red_kernel=rKern, nonred_kernel=nKern, 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))
elif args.log_level == "critical" or args.log_level == "CRITICAL": logger.setLevel(logging.CRITICAL) for arg in vars(args): logger.info("{}: {}".format(arg, getattr(args, arg))) ## torch.backends.cudnn.enabled = False momentum = 0.5 learning_rate = 0.01 n_epochs = 3 training_data, testing_data, example_data, example_targets = getData() # Save network = Net() optimizer = optim.SGD(network.parameters(), lr=learning_rate, momentum=momentum) loss = 0 test(network, testing_data) for epoch in range(1, n_epochs + 1): train(epoch, network, optimizer, loss, training_data) test(network, testing_data) # Load continued_network = Net() continued_optimizer = optim.SGD(network.parameters(), lr=learning_rate, momentum=momentum)
import numpy as np import torch import torch.nn as nn import torch.nn.functional as F import torchvision import torchvision.transforms as transforms import matplotlib.pyplot as plt import torch.optim as optim from cnn import Net net = Net() PATH = './cifar_net.pth' transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') 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()
import numpy as np 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)
sample = trainDataset[0] angle = 22.5 sample_x = transform(sample) display(sample_x['image'], sample_x['keypoints']) print(sample['keypoints'][0][1]) ''' trainLoader = DataLoader(trainDataset, batch_size=batch_size, shuffle=True, num_workers=0) testLoader = DataLoader(testDataset, batch_size=1, shuffle=True, num_workers=0) model = Net() critirion = torch.nn.SmoothL1Loss() optimizer = optim.Adam(model.parameters(), lr=0.00001) print(model) epoch = 1000 model.cuda() model, optimizer = loadModel(model, optimizer) #train(epoch=epoch, train_loader=trainLoader, optimizer= optimizer, critirion=critirion, model=model, testLoader= testLoader, batch_size= batch_size) torch.save(model.state_dict(), 'SavedModels/pull_model_saved') #test(model=model, testLoader=testLoader, batch_size=batch_size, im_num=12) #model = loadModel(model) #weights = model.conv2.weight.data.numpy()
# function to show an image def imshow(img): img = img/2+0.5 npimg = img.numpy() plt.imshow(np.transpose(npimg, (1,2,0))) plt.show() if __name__ == '__main__': # dataiter = iter(trainloader) # images, labels = dataiter.next() # imshow(torchvision.utils.make_grid(images, padding=2)) # print(' '.join(classes[labels[j]] for j in range(4))) bn = False net = Net(bn) criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") if torch.cuda.device_count() > 1: net = nn.DataParallel(net) net.to(device) # training the network for epoch in range(2): running_loss = 0.0 for i, data in enumerate(trainloader, 0): inputs, labels = data inputs, labels = inputs.to(device), labels.to(device)
# hyperparameters (start with r = for reducing layer, start with n = for nonreducing layer) lr_rate = float(param["learning_rate"]) rKern = int(param["reducing_conv_kernel"]) nKern = int(param["nonreducing_conv_kernel"]) rStride = int(param["reducing_stride"]) nStride = int(param["nonreducing_stride"]) rOut = int(param["reducing_out"]) nOut = int(param["nonreducing_out"]) # epoch hyperparameters num_epochs = int(param["num_epochs_per_batch"]) disp_epochs = param["display_epochs"] # constructing a model (converting model to double precision) model = Net(red_kernel=rKern, nonred_kernel=nKern, 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') # storing the training and test loss values obj_vals = [] #load test dataset t_img, t_nrg = loadimg(imdir + "\\" + files[-1])
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
def __init__(self, path, small): self.net = Net() self.path = path self.small = small self.net.load_state_dict(torch.load(path))
def __execute(self, model: cnn.Net, image_paths: List[str]): processed_data = { 'vanilla': [], 'deconv': [], 'gbp': [], 'gcam': [], 'ggcam': [], } device = next(model.parameters()).device model.eval() images, raw_images = load_images(image_paths, self.input_size) images = torch.stack(images).to(device) cls_num = len(self.classes) save_dir = Path(self.save_dir) save_dir.mkdir(parents=True, exist_ok=True) # --- Vanilla Backpropagation --- bp = BackPropagation(model=model) probs, ids = bp.forward(images) # sorted # --- Deconvolution --- deconv = None if self.is_deconv: deconv = Deconvnet(model=model) _ = deconv.forward(images) # --- Grad-CAM / Guided Backpropagation / Guided Grad-CAM --- gcam = None gbp = None if self.is_gradcam: gcam = GradCAM(model=model) _ = gcam.forward(images) gbp = GuidedBackPropagation(model=model) _ = gbp.forward(images) # probs = probs.detach().cpu().numpy() # to numpy # ids_np = ids.detach().cpu().numpy() # to numpy pbar = tqdm(range(cls_num), total=cls_num, ncols=100, bar_format='{l_bar}{bar:30}{r_bar}', leave=False) pbar.set_description('Grad-CAM') for i in pbar: if self.is_vanilla: bp.backward(ids=ids[:, [i]]) gradients = bp.generate() # Save results as image files for j in range(len(images)): # fmt = '%d-{}-%s.png' % (j, self.classes[ids_np[j, i]]) # print("\t#{}: {} ({:.5f})".format(j, classes[ids[j, i]], probs[j, i])) # append _grad = get_gradient_data(gradients[j]) processed_data['vanilla'].append(_grad) # save as image # _p = save_dir.joinpath(fmt.format('vanilla')) # save_gradient(str(_p), gradients[j]) if self.is_deconv: deconv.backward(ids=ids[:, [i]]) gradients = deconv.generate() for j in range(len(images)): # fmt = '%d-{}-%s.png' % (j, self.classes[ids_np[j, i]]) # print("\t#{}: {} ({:.5f})".format(j, classes[ids[j, i]], probs[j, i])) # append _grad = get_gradient_data(gradients[j]) processed_data['deconv'].append(_grad) # save as image # _p = save_dir.joinpath(fmt.format('deconvnet')) # save_gradient(str(_p), gradients[j]) # Grad-CAM / Guided Grad-CAM / Guided Backpropagation if self.is_gradcam: gbp.backward(ids=ids[:, [i]]) gradients = gbp.generate() # Grad-CAM gcam.backward(ids=ids[:, [i]]) regions = gcam.generate(target_layer=self.target_layer) for j in range(len(images)): # fmt = '%d-{}-%s.png' % (j, self.classes[ids_np[j, i]]) # print("\t#{}: {} ({:.5f})".format(j, classes[ids[j, i]], probs[j, i])) # append _grad = get_gradient_data(gradients[j]) processed_data['gbp'].append(_grad) _grad = get_gradcam_data(regions[j, 0], raw_images[j]) processed_data['gcam'].append(_grad) _grad = get_gradient_data(torch.mul(regions, gradients)[j]) processed_data['ggcam'].append(_grad) # save as image - Guided Backpropagation # _p = save_dir.joinpath(fmt.format('guided-bp')) # save_gradient(str(_p), gradients[j]) # save as image - Grad-CAM # _p = save_dir.joinpath(fmt.format(f'gradcam-{self.target_layer}')) # save_gradcam(str(_p), regions[j, 0], raw_images[j]) # save as image - Guided Grad-CAM # _p = save_dir.joinpath(fmt.format(f'guided_gradcam-{self.target_layer}')) # save_gradient(str(_p), torch.mul(regions, gradients)[j]) # Remove all the hook function in the 'model' bp.remove_hook() if self.is_deconv: deconv.remove_hook() if self.is_gradcam: gcam.remove_hook() gbp.remove_hook() return processed_data
def model(input): net = Net() net = loadModel(net) return net(input)
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)
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2) classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') def imshow(img): img = img / 2 + 0.5 npimg = img.numpy() plt.imshow(np.transpose(npimg, (1,2,0))) plt.show() dataiter = iter(trainloader) images, labels = dataiter.next() net = Net() # define loss function and optimizer criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) for epoch in range(2): running_loss = 0.0 for i, data in enumerate(trainloader, 0): inputs, labels = data optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step()
acc += partial_acc #accuracy on whole test set acc /= (nbs * bs) print('Test accuracy on this epoch: {}'.format(acc)) test_acc_values.append(acc) #save model torch.save(net.state_dict(), 'models/' + model_name) return lr_values, running_loss_values, train_acc_values, test_acc_values if __name__ == '__main__': net = Net() print(net) lr_values, running_loss_values, train_acc_values, test_acc_values = make_iterations( net, lr_ini) plt.plot(running_loss_values) plt.ylim((0, 5)) plt.title('Loss over iterations') plt.show() plt.plot(lr_values) plt.ylim((0, 20 * lr_ini)) plt.title('Learning rate over iterations') plt.show()
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
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')
def main(): try: args, check = argvcontrol() if check: if platform.system() == "Windows" or platform.system() == "win32": args.model = os.path.abspath(".") + "\\" + args.model args.path = os.path.abspath(".") + "\\" + args.path else: args.model = os.path.abspath(".") + "/" + args.model args.path = os.path.abspath(".") + "/" + args.path classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') net = Net() if args.cuda: net.cuda() if args.model and args.training: if not os.path.exists(args.model): print("Model will be created") else: loadModel(net, args.model) elif args.model and not args.training: if os.path.exists(args.model): loadModel(net, args.model) else: print( "Invalid model checkpoint file. Please enter a valid path." ) test_transform, train_transform = getTransformtions() if args.training: trainloader, testloader, criterion = CIFAR10Init( args.cuda, args.path, int(args.batch_size), int(args.workers)) if trainloader and testloader: frequency = int(len(trainloader) / 4) training(net, args.cuda, int(args.epochs), trainloader, frequency, criterion, float(args.learning_rate), int(args.batch_size), int(args.workers), args.model) else: exit() elif args.image: _class = recognition(net, args.image, test_transform, classes) if _class: print("\nClassification: %s\n" % (_class)) else: trainloader, testloader, criterion = CIFAR10Init( args.cuda, args.path, int(args.batch_size), int(args.workers)) if trainloader and testloader: validation(net, args.cuda, testloader, classes, args.model, int(args.batch_size)) else: exit() else: print("\nTraining: python image-recognizer.py --train") print("Validation: python image-recognizer.py --no-train") print( "Recognition: python image-recognizer.py --image PATH_TO_IMAGE" ) print( "You can set parameters for this operations. Add --help for more informations.\n" ) except (KeyboardInterrupt, SystemExit): pass
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")
acc += partial_acc #accuracy on whole test set acc /= (nbs * bs) print('Test accuracy on this epoch: {}'.format(acc)) test_acc_values.append(acc) #save model torch.save(net.state_dict(), 'models/' + model_name) return lr_values, running_loss_values, train_acc_values, test_acc_values if __name__ == '__main__': net = Net() net.cuda() print(net) lr_values, running_loss_values, train_acc_values, test_acc_values = make_iterations( net, lr_ini) plt.plot(running_loss_values) plt.ylim((0, 5)) plt.title('Loss over iterations') plt.show() plt.plot(lr_values) plt.ylim((0, 20 * lr_ini)) plt.title('Learning rate over iterations')
def main(model: cnn.Net, classes: List[str], input_size: Tuple[int, int]): # print("Mode:", ctx.invoked_subcommand) # classes = ['crossing', 'klaxon', 'noise'] # input_size = (60, 60) # model = cnn.Net(input_size) device = next(model.parameters()).device # model.to(device) model.eval() image_paths = [ './recognition_datasets/Images/crossing/crossing-samp1_3_4.jpg', './recognition_datasets/Images/crossing/crossing-samp1_3_3.jpg' ] images, raw_images = load_images(image_paths, input_size) images = torch.stack(images).to(device) bp = BackPropagation(model=model) probs, ids = bp.forward(images) # sorted ids = ids.cpu().numpy() # numpy gcam = GradCAM(model=model) _ = gcam.forward(images) gbp = GuidedBackPropagation(model=model) _ = gbp.forward(images) topk = 3 target_layer = 'conv5' output_dir = Path('results') output_dir.mkdir(parents=True, exist_ok=True) for i in range(topk): # Guided Backpropagation gbp.backward(ids=ids[:, [i]]) gradients = gbp.generate() # Grad-CAM gcam.backward(ids=ids[:, [i]]) regions = gcam.generate(target_layer=target_layer) for j in range(len(images)): name_fmt = f'{j}-' + '{}' + f'-{classes[ids[j, i]]}.png' print("\t#{}: {} ({:.5f})".format(j, classes[ids[j, i]], probs[j, i])) # Guided Backpropagation path = output_dir.joinpath(name_fmt.format('guided')).as_posix() save_gradient(filename=path, gradient=gradients[j]) # Grad-CAM path = Path(output_dir, name_fmt.format(f'gradcam-{target_layer}')).as_posix() save_gradcam(filename=path, gcam=regions[j, 0], raw_image=raw_images[j]) # Guided Grad-CAM path = Path( output_dir, name_fmt.format(f'guided_gradcam-{target_layer}')).as_posix() save_gradient(filename=path, gradient=torch.mul(regions, gradients)[j])