def infer(test_path, output_path, config): '''Train the network :param test_path: Test data directory path :param output_path: Output directory path :param config: config parser ''' for name in os.listdir(test_path): # Load image image = preprocess.load_image(os.path.join(test_path, name)) net_input = preprocess.preinference(image, config) # Create model model = get_model(config, net_input[0].shape) # Load model load_model(model, config) # Make prediction prediction = model.predict(net_input, verbose=1)[0] # Clip result prediction = preprocess.postinference(config, prediction, image) # Save images preprocess.save_image(name, prediction, output_path)
def main(): if len(sys.argv) < 3: raise RuntimeError( 'Command Line Argument Must be (sketch file, style file)') style_f = './data/styles/%s' % sys.argv[2] test_f = './data/test/%s' % sys.argv[1] filename = sys.argv[1][:-4] + sys.argv[2][:-4] + '.png' style = Image.open(style_f).convert('RGB') style = transforms.Resize((512, 512))(style) style_pil = style test = Image.open(test_f).convert('RGB') test_pil = transforms.Resize((512, 512))(test) transform = transforms.Compose( [transforms.Resize((512, 512)), transforms.ToTensor()]) test = transform(test) test = scale(test) test = test.unsqueeze(0).to(device) to_pil = transforms.ToPILImage() try: images = list(crop_region(style)) result = {} for i, img in enumerate(images, 1): colors = cgm.extract(img, topk + 1) result[str(i)] = { '%d' % i: get_rgb(colors[i]) for i in range(1, topk + 1) } color_tensor = make_colorgram_tensor(result) color_tensor = color_tensor.unsqueeze(0).to(device) fakeB, _ = model(test, color_tensor) fakeB = fakeB.squeeze(0) fakeB = re_scale(fakeB.detach().cpu()) fakeB = to_pil(fakeB) result_image = Image.new('RGB', (512 * 3, 512)) result_image.paste(test_pil, (512 * 0, 0, 512 * 1, 512)) result_image.paste(style_pil, (512 * 1, 0, 512 * 2, 512)) result_image.paste(fakeB, (512 * 2, 0, 512 * 3, 512)) save_image(result_image, os.path.join(out_root, filename)) except IndexError: exit(1)
attentions = list(map(lambda img: interpolate(img), attentions)) result = Image.new('RGBA', (4 * 512, 512)) styleB = styleB.squeeze() fakeB = fakeB.squeeze() imageA = imageA.squeeze() imageB = imageB.squeeze() imageA = to_pil(re_scale(imageA).detach().cpu()) imageB = to_pil(re_scale(imageB).detach().cpu()) styleB = to_pil(re_scale(styleB).detach().cpu()) fakeB = to_pil(re_scale(fakeB).detach().cpu()) result.paste(imageA, (0, 0)) result.paste(styleB, (512, 0)) result.paste(fakeB, (512 * 2, 0)) result.paste(imageB, (512 * 3, 0)) figure = Image.new('RGB', (9 * 512, 512)) figure.paste(imageA, (0, 0)) figure.paste(styleB, (512 * 1, 0)) figure.paste(attentions[0], (512 * 2, 0)) figure.paste(attentions[1], (512 * 3, 0)) figure.paste(attentions[2], (512 * 4, 0)) figure.paste(attentions[3], (512 * 5, 0)) figure.paste(attentions[4], (512 * 6, 0)) figure.paste(fakeB, (512 * 7, 0)) figure.paste(imageB, (512 * 8, 0)) save_image(figure, 'attention_%03d' % i, out_root)
styleA, styleB = styleB, styleA imageA = imageA.unsqueeze(0).to(device) imageB = imageB.unsqueeze(0).to(device) styleB = styleB.unsqueeze(0).to(device) colors = colors.unsqueeze(0).to(device) with torch.no_grad(): fakeB, _ = model( imageA, colors, ) result = Image.new('RGB', (4 * 512, 512)) styleB = styleB.squeeze() fakeB = fakeB.squeeze() imageA = imageA.squeeze() imageB = imageB.squeeze() imageA = to_pil(re_scale(imageA).detach().cpu()) imageB = to_pil(re_scale(imageB).detach().cpu()) styleB = to_pil(re_scale(styleB).detach().cpu()) fakeB = to_pil(re_scale(fakeB).detach().cpu()) result.paste(imageA, (0, 0)) result.paste(styleB, (512, 0)) result.paste(fakeB, (512 * 2, 0)) result.paste(imageB, (512 * 3, 0)) save_image(result, 'result_%03d' % i, out_root)
import tesserocr from PIL import Image from preprocess import read_image, preprocess, save_image print(tesserocr.tesseract_version()) # print tesseract-ocr version print(tesserocr.get_languages() ) # prints tessdata path and list of available languages print("Raw Image Output") image = Image.open('images/input/wegmans_20170912.png') print(tesserocr.image_to_text(image)) # print ocr text from image print("Processed Image Output") image = read_image('images/output/wegmans_20170912.png') processed_image = preprocess(image) save_image(processed_image, 'images/output/wegmans_20170912.png') processed_image = Image.open('images/output/wegmans_20170912.png') print(tesserocr.image_to_text(processed_image)) # print ocr text from image
def predict_tile(model_name, tile_file, save_name, model_file=None, batch_size=8): """Predict Tile Predict and save a terrain classification label for an input image tile file. # Arguments model: String. The name of an implemented model. tile_file: String. The filename of the input image tile file. save_name: String. Filename to save predicted image. model_file: String. Filename of Model file as h5 format. If not specified, uses default filename. batch_size: Integer. Number of patches in each batch. """ print("- Loading configuration...") if model_name in models_default_params: default_params = models_default_params[model_name] else: print("model_name not specified. Loading generic configuration.") default_params = models_default_params['generic'] custom_objects = default_params['custom_objects'] stride = default_params['stride'] patch_size = default_params['patch_size'] if model_file is None: model_file = default_params['default_path'] if stride > patch_size: print("Warning: stride of {} is greater than patch size of {}.".format( stride, patch_size)) print("- Configuration loaded.") print("- Loading tile...") if not os.path.isfile(tile_file): print("Error: {} is not a valid file.".format(tile_file)) return patch_gen = preprocess.load_patch_batch(tile_file, batch_size, patch_size=patch_size, stride=stride) steps = preprocess.calc_nb_batches(tile_file, batch_size, patch_size=patch_size, stride=stride) height, length = preprocess.get_image_size(tile_file) print("- Tile loaded.") print("- Initialising model...") model = keras.models.load_model(model_file, custom_objects=custom_objects) model.summary() print("- Model initialised.") print("- Predicting tile...") start = time.time() pred_b_coordinates = [] pred_b_probs = [] for i in range(steps): print("Batch {}/{}".format(i + 1, steps)) b_start = time.time() coordinates, patches = next(patch_gen) pred_b_coordinates.append(coordinates) pred_b_probs.append(model.predict_on_batch(patches)) b_duration = time.time() - b_start print("Batch predicted in {:.3f}s".format(b_duration)) duration = time.time() - start print("- Prediction complete in {:.3f}s.".format(duration)) print("- Averaging predictions...") start = time.time() tile = ProbabilityTile(height, length) predicted_coordinates = list( itertools.chain.from_iterable(pred_b_coordinates)) predicted_probs = list(itertools.chain.from_iterable(pred_b_probs)) for i in range(len(predicted_probs)): tile.merge_prob_patch(predicted_coordinates[i][0], predicted_coordinates[i][1], predicted_probs[i]) tile_label = tile.get_tile_label() duration = time.time() - start print("- Averaging complete in {:.3f}s".format(duration)) print("- Saving predicted tile...") preprocess.save_image(tile_label, save_name) print("- Saved.")
def validate(self, dataset, epoch, samples=3): # self.generator.eval() # self.discriminator.eval() length = len(dataset) # sample images idxs_total = [ random.sample(range(0, length - 1), samples * 2) for _ in range(epoch) ] for j, idxs in enumerate(idxs_total): styles = idxs[samples:] targets = idxs[0:samples] result = Image.new( 'RGB', (5 * self.resolution, samples * self.resolution)) toPIL = transforms.ToPILImage() G_loss_gan = [] G_loss_l1 = [] D_loss_real = [] D_loss_fake = [] l1_loss = self.losses['L1'] gan_loss = self.losses['GAN'] for i, (target, style) in enumerate(zip(targets, styles)): sub_result = Image.new('RGB', (5 * self.resolution, self.resolution)) imageA, imageB, _ = dataset[target] styleA, styleB, colors = dataset[style] if self.args.mode == 'B2A': imageA, imageB = imageB, imageA styleA, styleB = styleB, styleA imageA = imageA.unsqueeze(0).to(self.device) imageB = imageB.unsqueeze(0).to(self.device) styleB = styleB.unsqueeze(0).to(self.device) colors = colors.unsqueeze(0).to(self.device) with torch.no_grad(): fakeB, _ = self.generator( imageA, colors, ) fakeAB = torch.cat([imageA, fakeB], 1) realAB = torch.cat([imageA, imageB], 1) G_loss_l1.append(l1_loss(fakeB, imageB).item()) G_loss_gan.append( gan_loss(self.discriminator(fakeAB), True).item()) D_loss_real.append( gan_loss(self.discriminator(realAB), True).item()) D_loss_fake.append( gan_loss(self.discriminator(fakeAB), False).item()) styleB = styleB.squeeze() fakeB = fakeB.squeeze() imageA = imageA.squeeze() imageB = imageB.squeeze() colors = colors.squeeze() imageA = toPIL(re_scale(imageA).detach().cpu()) imageB = toPIL(re_scale(imageB).detach().cpu()) styleB = toPIL(re_scale(styleB).detach().cpu()) fakeB = toPIL(re_scale(fakeB).detach().cpu()) # synthesize top-4 colors color1 = toPIL(re_scale(colors[0:3].detach().cpu())) color2 = toPIL(re_scale(colors[3:6].detach().cpu())) color3 = toPIL(re_scale(colors[6:9].detach().cpu())) color4 = toPIL(re_scale(colors[9:12].detach().cpu())) color1 = color1.rotate(90) color2 = color2.rotate(90) color3 = color3.rotate(90) color4 = color4.rotate(90) color_result = Image.new('RGB', (self.resolution, self.resolution)) color_result.paste( color1.crop((0, 0, self.resolution, self.resolution // 4)), (0, 0)) color_result.paste( color2.crop((0, 0, self.resolution, self.resolution // 4)), (0, self.resolution // 4)) color_result.paste( color3.crop((0, 0, self.resolution, self.resolution // 4)), (0, self.resolution // 4 * 2)) color_result.paste( color4.crop((0, 0, self.resolution, self.resolution // 4)), (0, self.resolution // 4 * 3)) sub_result.paste(imageA, (0, 0)) sub_result.paste(styleB, (self.resolution, 0)) sub_result.paste(fakeB, (2 * self.resolution, 0)) sub_result.paste(imageB, (3 * self.resolution, 0)) sub_result.paste(color_result, (4 * self.resolution, 0)) result.paste(sub_result, (0, 0 + self.resolution * i)) print( 'Validate D_loss_real = %f, D_loss_fake = %f, G_loss_l1 = %f, G_loss_gan = %f' % ( sum(D_loss_real) / samples, sum(D_loss_fake) / samples, sum(G_loss_l1) / samples, sum(G_loss_gan) / samples, )) save_image( result, 'deepunetpaint_%03d_%02d' % (epoch, j), self.save_path, )