def train(train_loader, model, criterion, optimizer, epoch): model.train() batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() end = time.time() for i, (img, target) in enumerate(train_loader): # measure data loading time if img is None: continue data_time.update(time.time() - end) encoded_target = Variable(utils.soft_encode_ab(target).float(), requires_grad=False).cuda() var = Variable(img.float(), requires_grad=True).cuda() # compute output output = model(var) # record loss loss = criterion(output, encoded_target) if torch.isnan(loss): print('NaN value encountered in loss.') continue # measure accuracy and record loss #prec1, = accuracy(output.data, target) losses.update(loss.data, var.size(0)) # compute gradient and do SGD step backwardTime = time.time() optimizer.zero_grad() loss.backward() optimizer.step() # measure elapsed time batch_time.update(time.time() - end) end = time.time() if i % args.print_freq == 0: print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format( epoch, i, len(train_loader), batch_time=batch_time, data_time=data_time, loss=losses)) if (i+1) % 5000 == 0: print("Saving checkpoint...") save_checkpoint({ 'epoch': epoch, 'state_dict': model.state_dict(), }, args.reduced) if (i+1) % 1000 == 0: start = time.time() batch_num = np.maximum(args.batch_size//4,2) idx = i + epoch*len(train_loader) imgs = utils.getImages(img, target, output.detach().cpu(), batch_num) writer.add_image('data/imgs_gen', imgs, idx) print("Img conversion time: ", time.time() - start) writer.add_scalar('data/loss_train', losses.avg, i + epoch*len(train_loader))
def __init__(self, input_path, output_path): self.n = FeedForwardNetwork() self.pix_size = 50 self.input_value = utils.getImages(utils.readLines(input_path)) self.output_value = utils.readLines(output_path) self.inputUnits = self.pix_size * self.pix_size self.nbHiddenLayers = 1 self.hiddenUnits = 500 self.outputUnits = len(results) self.ds = SupervisedDataSet( self.pix_size * self.pix_size, len(results)) self.initializeDataSet() self.initilizeNetwork() self.trainingOnDataSet()
def extract_pieces(path, needs_filter, threshold, piece_size): pieces = [] color_images = utils.getImages(path) if needs_filter: blured_images = utils.median_blur(color_images, 5) bws = utils.color_to_bw(blured_images, threshold) else: bws = utils.color_to_bw(color_images, threshold) bws = utils.filter(bw, 2) for bw, color_image in zip(bws, color_images): (_, cnts, _) = cv2.findContours(bw.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) for contour in cnts: bordersize = 15 x, y, w, h = cv2.boundingRect(contour) if (w < piece_size or h < piece_size): continue new_bw = np.zeros((h + 2 * bordersize, w + 2 * bordersize), dtype='uint8') contours_to_draw = [] contours_to_draw.append( utils.translate_contour(contour, bordersize - x, bordersize - y)) cv2.drawContours(new_bw, np.asarray(contours_to_draw), -1, 255, -1) w += bordersize * 2 h += bordersize * 2 x -= bordersize y -= bordersize mini_color = color_image[y:y + h, x:x + w] mini_bw = new_bw mini_color = mini_color.copy() mini_bw = mini_bw.copy() one_piece = piece.create_piece(mini_color, mini_bw, piece_size) pieces.append(one_piece) return pieces
def train(train_loader, model_G, model_D, criterion_G, criterion_GAN, optimizer_G, optimizer_D, epoch): model_D.train() model_G.train() batch_time = AverageMeter() data_time = AverageMeter() losses_L2 = AverageMeter() losses_G = AverageMeter() losses_D = AverageMeter() end = time.time() for i, (real, img_L, target) in enumerate(train_loader): ## Code for forward - backward - update pass in Generator and Discriminator # Inspired by https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix data_time.update(time.time() - end) var = Variable(img_L.float(), requires_grad=True).cuda() real = Variable(real.float(), requires_grad=True).cuda() target_class = Variable(utils.soft_encode_ab(target).float(), requires_grad=False).cuda() # compute output G(L) output = model_G(var) # Update gradients for Discriminator model_D.module.set_grads(True) optimizer_D.zero_grad() # Fake loss term output_up = interpolate(utils.decode(output), scale_factor=4, mode='bilinear', recompute_scale_factor=True, align_corners=True) fake_img = torch.cat([var, output_up], 1) fake_prob = model_D(fake_img.detach()) loss_D_fake = criterion_GAN(fake_prob, False) # Real loss term real_prob = model_D(real) loss_D_real = criterion_GAN(real_prob, True) loss_D = (loss_D_real + loss_D_fake) * 0.5 if torch.isnan(loss_D): print('NaN value encountered in loss_D.') continue loss_D.backward() optimizer_D.step() # Update gradients for Generator model_D.module.set_grads(False) optimizer_G.zero_grad() fake_prob = model_D(fake_img) # Fool the discriminator loss_G_GAN = criterion_GAN(fake_prob, True) # Regressor loss term loss_G_L2 = criterion_G(output, target_class) loss_G = loss_G_GAN + loss_G_L2 * 10 if torch.isnan(loss_G): print('NaN value encountered in loss_G.') continue loss_G.backward() optimizer_G.step() losses_D.update(loss_D.data, var.size(0)) losses_G.update(loss_G_GAN.data, var.size(0)) losses_L2.update(loss_G_L2.data, var.size(0)) batch_time.update(time.time() - end) end = time.time() if i % args.print_freq == 0: print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss: G {loss_g.val:.4f} ({loss_g.avg:.4f})\t' 'D {loss_d.val:.4f} ({loss_d.avg:.4f})\t'.format( epoch, i, len(train_loader), batch_time=batch_time, data_time=data_time, loss_g=losses_G, loss_d=losses_D)) if (i + 1) % 5000 == 0: print("Saving checkpoint...") save_checkpoint( { 'epoch': epoch + 1, 'state_dict_G': model_G.state_dict(), 'state_dict_D': model_D.state_dict(), }, args.reduced) if (i + 1) % 1000 == 0: start = time.time() batch_num = np.maximum(args.batch_size // 4, 2) idx = i + epoch * len(train_loader) imgs = utils.getImages(var.detach(), target.cuda(), output.detach(), batch_num, do_decode=True) writer.add_image('data/imgs_gen', imgs, idx) print("Img conversion time: ", time.time() - start) writer.add_scalar('data/L2_loss_train', losses_L2.avg, i + epoch * len(train_loader)) writer.add_scalar('data/D_loss_train', losses_D.avg, i + epoch * len(train_loader)) writer.add_scalar('data/G_loss_train', losses_G.avg, i + epoch * len(train_loader))
import numpy as np import matplotlib.pyplot as plt import matplotlib as matplot import gzip import struct from sklearn import datasets from sklearn.neural_network import MLPClassifier from sklearn.metrics import accuracy_score from sklearn.externals import joblib import utils train_size = 60000 test_size = 10000 x_train = utils.getImages('./data/train-images-idx3-ubyte.gz', train_size) #print(len(x)) #print(x[1].shape) #image = np.reshape(x[2], (28,28)) #plt.imshow(image, cmap='gray', vmin=0, vmax=255) #plt.show() y_train = utils.getLabels("./data/train-labels-idx1-ubyte.gz", train_size) #### Build the model mlp = MLPClassifier(hidden_layer_sizes=(15, ), activation='logistic', alpha=1e-4, solver='sgd', tol=1e-4, random_state=1, learning_rate_init=.1, verbose=True)
def images(): """ View for docker images """ images = {"images": getImages()} return jsonify(images)
2.1 Traverses the image to detect the faces in the image using Viola Jones Classifier 2.2 Gets the locations in which the image has Faces 2.3 Adds the locations in the global JSON_LIST 3. Dumps the JSON_LIST to results.json 4. STOP """ args = parse_args() TestImgsFolder = args.string TestImgs = getImages(TestImgsFolder) for i in range(0, len(TestImgs)): imgtest = cv2.imread(TestImgsFolder + '/' + TestImgs[i]) gray = cv2.cvtColor(imgtest, cv2.COLOR_RGB2GRAY) locations = getFaceLocations(gray) if locations is not None: locations = nonMaximalSupression(np.array(locations), 0.25) addToJson(TestImgs[i], locations)
import predict import utils x_test = utils.getImages('./data/t10k-images-idx3-ubyte.gz', 100) y_test = utils.getLabels('./data/t10k-labels-idx1-ubyte.gz', 100) pred = predict.predictMLPClassifier(x_test[98]) print("Pred: " + str(pred) + ", Actual: " + str(y_test[98]))
suggestedLinesField.append('Company') elif checkForPhone(line): suggestedLinesField.append('Phone') elif checkFax(line): suggestedLinesField.append('Fax') elif checkWebsite(line): suggestedLinesField.append('Website') else: suggestedLinesField.append('') lineCount += 1 lineCount = 0 suggestedFields.append(suggestedLinesField) return suggestedFields if __name__ == "__main__": imgs = utils.getImages('../../stanford_business_cards/photos/', 5) # imgs = utils.getImages('../our_cards/', 8) DEBUG = True # img = utils.readImage('../../stanford_business_cards/photos/004.jpg') # imgs = [('',img)] # utils.display(imgs) good, cardImg = findCard.findCard(imgs[4][1]) utils.display([('card',cardImg)]) regions, text = processCard(cardImg) processedCard = drawRegions(cardImg, regions) suggestedFields = guessFields(regions, text) utils.display([('card',processedCard)]) # scores = [] # for imgName, img in imgs: # # regions, text = processCard(img)