net.get_model().summary() img = cv2.imread('div2k_test.png') img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) r = DATA.patch_size - img.shape[0] % DATA.patch_size c = DATA.patch_size - img.shape[1] % DATA.patch_size img = np.pad(img, [(0, r), (0, c), (0, 0)], 'constant') Image.fromarray(img).save("div2k_padded_test.png") lr_img = cv2.resize(img, (int(img.shape[1] / 2), int(img.shape[0] / 2)), cv2.INTER_CUBIC) Image.fromarray(lr_img).save("div2k_padded_test_lr.png") p, r, c = DATA.patchify(lr_img, scale=2) for i in range(chk + 1, tryout): print("tryout no: ", i) samplev = np.random.random_integers(0, DATA.training_patches_2x.shape[0] - 1, sample) net.fit(DATA.training_patches_2x[samplev], DATA.training_patches_Y[samplev], batch_size, epochs) net.get_model().save_weights('model_iter' + str(i) + '.h5') g = net.get_model().predict(np.array(p)) gen = DATA.reconstruct(g, r, c, scale=1) gen[gen > 255] = 255 gen[gen < 0] = 0 Image.fromarray(gen).save("main_div2k_gen_" + str(i) + ".jpeg") print("Reconstruction Gain:", PSNRLossnp(img, gen))
net.visualize() net.get_model().summary() # Temp stuff for barcode ##### os.mkdir('bargen') loc = '/home/sanchit/Barcodes/HR/detected_barcodes' for f in os.listdir(loc): if not os.path.isdir(f): filename = loc + '/' + f img = cv2.imread(filename) r = 32 - img.shape[0] % 32 c = 32 - img.shape[1] % 32 img = np.pad(img, [(0, r), (0, c), (0, 0)], 'constant') gz, r2, c2 = DATA.patchify(img, scale=scale) gz = net.get_model().predict(np.array(gz)) genz = DATA.reconstruct(gz, r2, c2, scale=1) Image.fromarray(genz).save('bargen/' + f) image_name = values.test_image try: img = cv2.imread(image_name) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) except cv2.error as e: print("Bad image path check the name or path !!") exit() if not zoom: r = DATA.patch_size - img.shape[0] % DATA.patch_size c = DATA.patch_size - img.shape[1] % DATA.patch_size img = np.pad(img, [(0, r), (0, c), (0, 0)], 'constant')
r = DATA.patch_size - img.shape[0] % DATA.patch_size c = DATA.patch_size - img.shape[1] % DATA.patch_size img = np.pad(img, [(0,r),(0,c),(0,0)] , 'constant') Image.fromarray(img).save("div2k_padded_test.png") lr_img = cv2.resize(img , (int(img.shape[1]/2),int(img.shape[0]/2)) ,cv2.INTER_CUBIC) Image.fromarray(lr_img).save("div2k_padded_test_lr.png") p , r , c = DATA.patchify(lr_img,scale=2) #p = np.array(p) / 255 p = np.array(p) first_image_Y = DATA.reconstruct(DATA.training_patches_Y , r , c , scale=1) first_image_X = DATA.reconstruct(DATA.training_patches_2x , r , c , scale=2) Image.fromarray(first_image_X).save("first_image_X.jpeg") Image.fromarray(first_image_Y).save("first_image_Y.jpeg") for i in range(chk+1,tryout): print("tryout no: ",i) samplev = np.random.random_integers(0 , DATA.training_patches_2x.shape[0]-1 , sample) normalised_2x = DATA.training_patches_2x[samplev] normalised_Y = DATA.training_patches_Y[samplev]
for i in range(chk + 1, tryout): gen_acc = 0.0 dic_acc = 0.0 print("tryout no: ", i) # samplev = np.random.random_integers(0 , x.shape[0]-1 , sample) if adv_lambda > 0.0: net.fit_discriminator(x, x2, x4, x8, batch_size, epoch=epochs) net.fit(x, x2, x4, x8, batch_size, epoch=epochs) net.get_model().save_weights('model_iter' + str(i) + '.h5') g = net.predict(np.array(p)) gen2x = DATA.reconstruct(g[0], r, c, scale=4) gen4x = DATA.reconstruct(g[1], r, c, scale=2) gen8x = DATA.reconstruct(g[2], r, c, scale=1) d = 'Results/' + str(i) if not os.path.isdir(d): os.mkdir(d) Image.fromarray(gen2x).save(d + "/test_2x_gen_.png") Image.fromarray(gen4x).save(d + "/test_4x_gen_.png") Image.fromarray(gen8x).save(d + "/test_8x_gen_.png") print("Reconstruction Gain:", PSNRLossnp(img, gen8x)) else: if zoom: gz, r2, c2 = DATA.patchify(img, scale=8) gz = net.get_model().predict(np.array(gz))[2] genz = DATA.reconstruct(gz, r2, c2, scale=1) Image.fromarray(genz).save("test_image_zoomed_8x.png")