def test(): model.load_state_dict(torch.load('./autoencoder_model.pth')) model.eval() count = 0 loss_list = [] target_t = [] train_loss = 0 with torch.no_grad(): for batch_idx, data in enumerate(train_loader): img = data img = Variable(img, requires_grad=False).cuda() recon_batch = model(img) loss = ssim(recon_batch, img) print(loss) loss_list.append(loss.item()) train_loss += loss print('Loss/Train avg loss: {} / {}'.format(loss, train_loss)) x = to_img(img.cpu().data) x_hat = to_img(recon_batch.cpu().data) #save_image(x, './test_vae/x_{}.png'.format(count)) #save_image(x_hat, './test_vae/x_hat_{}.png'.format(count)) count += 1 torch.save(loss_list, './train_loader_loss.pt') torch.save(target_t, './test_target_t.pt')
def test_draw_text(self): chart_renderer = RenderChart(self.chart_cfg) text = self.chart_cfg['title_text']['text'] chart_renderer._draw_text(text, self.chart_cfg['title_text'], 100, 50) truth = Image.open(path.join(settings.TEST_DIR, "charts/test_all_text_draw_text.png")) diff = ssim(chart_renderer.chart, truth) self.assertGreaterEqual(diff, 0.97)
def test_render(self): chart_renderer = RenderChart(self.chart_cfg) ret = chart_renderer.render_chart() # Compare to ground truth. truth = Image.open(path.join(settings.TEST_DIR, "charts/test_show_all_text.png")) diff = ssim(ret, truth) self.assertGreaterEqual(diff, 0.97)
def test_paste_tile_second_row(self): chart_renderer = RenderChart(self.chart_cfg) image = self.chart_cfg['images'][0][0] tile_width = self.chart_cfg['tile_sizes'][0][0] tile_height = self.chart_cfg['tile_sizes'][0][1] chart_renderer._paste_tile(11, image, 10, tile_width, tile_height) truth = Image.open(path.join(settings.TEST_DIR, "charts/test_all_text_tile_second_row.png")) diff = ssim(chart_renderer.chart, truth) self.assertGreaterEqual(diff, 0.97)
def compare_images(imageA, imageB): #finds the ssim value and returens it #converts images to greyscale to find the ssim value grayA = Image.open(imageA).convert('L') grayB = Image.open(imageB).convert('L') score = ssim(grayA, grayB) return score
def test_transform_image_default_crop(self): chart_renderer = RenderChart(self.chart_cfg) image = Image.open((path.join(settings.TEST_DIR, "images", self.chart_cfg['images'][0][0]['path']))) tile_width = self.chart_cfg['tile_sizes'][0][0] tile_height = self.chart_cfg['tile_sizes'][0][1] cropped = chart_renderer._transform_image(image, tile_width, tile_height) truth = Image.open(path.join(settings.TEST_DIR, "charts/test_transform_image_default_crop.png")) diff = ssim(cropped, truth) self.assertGreaterEqual(diff, 0.97)
def show(): decoded_imgs = torch.load('./recon_x.pt') x_train = torch.load('./img_orig.pt') # for batch_idx, data in enumerate(decoded_imgs): # x_train.append(data) n = 8 #how many digits we will display plt.figure(figsize=(20, 5), dpi=100) for i in range(n): # display original ax = plt.subplot(2, n, i + 1) plt.imshow(x_train[i].reshape(128, 128)) plt.gray() ax.get_xaxis().set_visible(True) ax.get_yaxis().set_visible(False) # SSIM Encode ax.set_title("Encode_Image") npImg = x_train[i] npImg = npImg.reshape((128, 128)) formatted = (npImg * 255 / np.max(npImg)).astype('uint8') img = Image.fromarray(formatted) # display reconstruction ax = plt.subplot(2, n, i + 1 + n) plt.imshow(decoded_imgs[i].reshape(128, 128)) plt.gray() ax.get_xaxis().set_visible(True) ax.get_yaxis().set_visible(False) # SSIM Decoded npDecoded = decoded_imgs[i] npDecoded = npDecoded.reshape((128, 128)) formatted2 = (npDecoded * 255 / np.max(npDecoded)).astype('uint8') decoded = Image.fromarray(formatted2) value = ssim(img, decoded) label = 'SSIM: {:.3f}' ax.set_title("Decoded_Image") ax.set_xlabel(label.format(value)) plt.show()
def test_image_upload(self): image_path = construct_image_path(str(abs(hash(self.eva_base64)))) response = self.client.post(self.url, self.eva_data, content_type="application/json", HTTP_X_REQUESTED_WITH='XMLHttpRequest') id = response['id'] db_image = ImageModel.objects.get(pk=id) saved_path = db_image.image_path saved_title = db_image.image_title saved_tags = db_image.tags.all() pil_saved_image = Image.open(path.join(settings.IMAGE_DIR, saved_path)) pil_original_image = Image.open( BytesIO(base64.b64decode(self.eva_base64))) correct_tags = [ Tag(tag="anime"), Tag(tag="90s"), Tag(tag="classic"), Tag(tag="gainax"), Tag(tag="hideaki anno"), Tag(tag="evangelion"), Tag(tag="neon genesis evangelion"), Tag(tag="shinseiki evangelion") ] diff = ssim(pil_saved_image, pil_original_image) self.assertEqual(response.status_code, 200) self.assertEqual(saved_title, self.eva_data['title']) self.assertEqual(response['path'], image_path) self.assertEqual(response['path'], saved_path) self.assertEqual(set(correct_tags), set(saved_tags)) self.assertEqual(diff, 1.0)
# display reconstruction ax = plt.subplot(2, n, i + 1 + n) plt.imshow(decoded_imgs[i].reshape(128, 128)) plt.gray() ax.get_xaxis().set_visible(True) ax.get_yaxis().set_visible(False) # SSIM Decoded npDecoded = decoded_imgs[i] npDecoded = npDecoded.reshape((128, 128)) formatted2 = (npDecoded * 255 / np.max(npDecoded)).astype('uint8') decoded = Image.fromarray(formatted2) value = ssim(img, decoded) label = 'SSIM: {:.3f}' ax.set_title("Decoded_Image") ax.set_xlabel(label.format(value)) plt.show()
def get_output(x_test): width = 128 height = 128 pixels = width * height * 1 x_test = x_test.astype('float32') / 255. x_test = np.reshape( x_test, (len(x_test), 128, 128, 1)) # adapt this if using `channels_first` image data format # print (x_train.shape) print(x_test.shape) input_shape = x_test.shape[1:] autoencoder = autoencoderModel(input_shape) # autoencoder.cuda() # TensorFlow wizardry import keras gpu_options = tf.GPUOptions(allow_growth=True) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) keras.backend.tensorflow_backend.set_session(sess) # k.tensorflow_backend.set_session(tf.Session(config=config)) autoencoder.load_weights( '/home/jbmai/try/modelServer/autoencoder_mayank/weights-improvement-39-0.18.hdf5' ) autoencoder.compile(optimizer='adam', loss=ssim_loss, metrics=[ssim_loss, 'accuracy']) decoded_imgs = autoencoder.predict(x_test) # weights-improvement-112-0.24 # cv2.imwrite("mayank.jpg",decoded_imgs) # n = 1 # how many digits we will display n = len(x_test) # how many digits we will display # plt.figure(figsize=(20, 5), dpi=100) finalValue = 0 second = 0 for i in range(n): npImg = x_test[i] npImg = npImg.reshape((128, 128)) formatted = (npImg * 255 / np.max(npImg)).astype('uint8') img = Image.fromarray(formatted) img1 = np.asarray(img) # cv2.imwrite("ResultNew/back/img_test"+name+"enc.jpg",img1) # display reconstruction # ax = plt.subplot(2, n, i + 1 + n) # plt.imshow(decoded_imgs[i].reshape(128, 128)) # plt.gray() # ax.get_xaxis().set_visible(True) # ax.get_yaxis().set_visible(False) # SSIM Decoded npDecoded = decoded_imgs[i] npDecoded = npDecoded.reshape((128, 128)) formatted2 = (npDecoded * 255 / np.max(npDecoded)).astype('uint8') decoded = Image.fromarray(formatted2) decoded1 = np.asarray(decoded) # cv2.imwrite("ResultNew/back/img_test"+name+"dec.jpg",decoded1) value = ssim(img, decoded) value2 = mse(img1, decoded1) finalValue = finalValue + value second = second + value2 return finalValue, second
def get_output(x_test, filename): width = 128 height = 128 pixels = width * height * 1 x_test = x_test.astype('float32') / 255. x_test = np.reshape( x_test, (len(x_test), 128, 128, 1)) # adapt this if using `channels_first` image data format # print (x_train.shape) print(x_test.shape) input_shape = x_test.shape[1:] autoencoder = autoencoderModel(input_shape) # autoencoder.cuda() # TensorFlow wizardry import keras gpu_options = tf.GPUOptions(allow_growth=True) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) keras.backend.tensorflow_backend.set_session(sess) # k.tensorflow_backend.set_session(tf.Session(config=config)) autoencoder.load_weights('../finalweights/we.hdf5') autoencoder.compile(optimizer='adam', loss=ssim_loss, metrics=[ssim_loss, 'accuracy']) decoded_imgs = autoencoder.predict(x_test) # weights-improvement-112-0.24 # cv2.imwrite("mayank.jpg",decoded_imgs) # n = 1 # how many digits we will display n = len(x_test) # how many digits we will display plt.figure(figsize=(20, 5), dpi=100) ssimValue = 0 mseValue = 0 m = 1 for i in range(n): # display original ax = plt.subplot(2, n, i + 1) plt.imshow(x_test[i].reshape(128, 128)) plt.gray() ax.get_xaxis().set_visible(True) ax.get_yaxis().set_visible(False) # SSIM Encode ax.set_title("Encode_Image") # name = n_test[i+1].split('/')[-1] npImg = x_test[i] npImg = npImg.reshape((128, 128)) formatted = (npImg * 255 / np.max(npImg)).astype('uint8') img = Image.fromarray(formatted) img1 = np.asarray(img) # cv2.imwrite("ResultNew/back/img_test"+name+"enc.jpg",img1) # display reconstruction ax = plt.subplot(2, n, i + 1 + n) plt.imshow(decoded_imgs[i].reshape(128, 128)) plt.gray() ax.get_xaxis().set_visible(True) ax.get_yaxis().set_visible(False) # SSIM Decoded npDecoded = decoded_imgs[i] npDecoded = npDecoded.reshape((128, 128)) formatted2 = (npDecoded * 255 / np.max(npDecoded)).astype('uint8') decoded = Image.fromarray(formatted2) decoded1 = np.asarray(decoded) value = ssim(img, decoded) # mse = np.square(np.subtract(img1,decoded1)).mean() mse2 = mse(img1, decoded1) ssimValue = ssimValue + value mseValue = mseValue + mse2 label = 'SSIM: {:.3f}' label1 = 'MSE: {:.3f}' ax.set_title("Decoded_Image") ax.set_xlabel(label.format(value) + "\n" + label1.format(mse2)) # ax.set_ylabel(label.format(value)) if i == n - 1: plt.savefig(filename.split(".jpg")[0] + "1.jpg") # m+=1 plt.show() return ssimValue