Related
I am trying to call my Face Recognition model implemented in keras, using flask API. I am unable to call the model using different cam urls as a parameter.
I am getting the following error:
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder_50:0", shape=(3, 3, 3, 32), dtype=float32) is not an element of this graph.
127.0.0.1 - - [23/Nov/2022 13:39:49] "GET /api/recognise?url=rtsp://admin:inndata123#10.10.5.202:554/cam/realmonitor?channel=1&subtype=0 HTTP/1.1" 500 -
I found that creating a new session for each thread, but I don't have any idea where to place those lines in my code.
# running db and email functions in background and parallalized action and bbox dist loops
import json
import os
import pickle
import cv2
import imutils
import dlib
import torch
import time
import numpy as np
import datetime
from pathlib import Path
import matplotlib.pyplot as plt
from PIL import Image, ImageFont, ImageDraw
from script.fx import prewhiten, l2_normalize
from keras.models import load_model
from scipy.spatial import distance
from mtcnn.mtcnn import MTCNN
from script.generate_data import generate_embeddings
import mysql.connector
from mysql.connector import (connection)
import smtplib
import mimetypes
from email.message import EmailMessage
message = EmailMessage()
import tensorflow as tf
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,
inter_op_parallelism_threads=2)
from flask import Flask, jsonify, request,render_template,Response
app = Flask(__name__)
global graph
graph = tf.get_default_graph()
sess = tf.Session(graph=graph, config=session_conf)
model_path = './data/model/facenet_keras.h5'
font_path = './data/font/Calibri Regular.ttf'
embedding_path = './data/arrays/embeddings.npz'
vars_path = './data/arrays/vars.npz'
curr_time = datetime.datetime.now()
time_date = curr_time.strftime('%Y-%m-%d %H:%M:%S')
only_date= curr_time.strftime('%Y-%m-%d')
login_time = curr_time.replace(hour=8, minute=0, second=0, microsecond=0)
logout_time = curr_time.replace(hour=17, minute=15, second=0, microsecond=0)
if os.path.exists(embedding_path) == True:
print('Loadings embeddings...')
loaded_embeddings = np.load(embedding_path)
embeddings, names = loaded_embeddings['a'], loaded_embeddings['b']
loaded_vars = np.load(vars_path)
slope, intercept = loaded_vars['a'], loaded_vars['b']
else:
print('Creatings embeddings...')
generate_embeddings()
loaded_embeddings = np.load(embedding_path)
embeddings, names = loaded_embeddings['a'], loaded_embeddings['b']
loaded_vars = np.load(vars_path)
slope, intercept = loaded_vars['a'], loaded_vars['b']
location='IE'
cam_id='Entrance-Cam'
frame_count = 0
frame_number = 0
bbox_centers = []
log_in = []
log_out = []
date_list = []
mins_lst = []
#app.route('/api/recognise')
def recognise():
url = request.args.get('url')
if url!=str(0):
subtype=request.args.get('subtype')
url=url+'&'+'subtype='+subtype
print(url)
else:url=int(url)
video_sources = cv2.VideoCapture(url)
detector = MTCNN()
model = load_model(model_path, compile=False)
graph = tf.get_default_graph()
def inner():
frame_count = 0
frame_number = 0
while 1:
start= time.time()
var, frame = video_sources.read()
if frame is not None:
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_small_frame = small_frame[:, :, ::-1]
# frame = cv2.resize(frame, (1500, 1000))
if frame_count % 10 == 0 and rgb_small_frame is not None:
faces = detector.detect_faces(rgb_small_frame) # result
#print(faces)
print('faces :',len(faces))
for result in faces:
x_face, y_face, w_face, h_face = result['box']
x_face = x_face * 4
y_face = y_face * 4
w_face = w_face * 4
h_face = h_face * 4
x_face2=w_face+x_face
y_face2=h_face+y_face
#face bbox tuples
face_tuple1=(x_face,y_face)
face_tuple2=(x_face2,y_face2)
#zone bbox tuples
zone_tuple1 = (950, 700)
zone_tuple2 = (2000, 1050)
# Margins for Face box
dw = 0.1 * w_face
dh = 0.2 * h_face
#center = (x_face + w_face // 2, y_face + h_face // 2)
#cv2.rectangle(frame, zone_tuple1, zone_tuple2, (255, 0, 0), 2)
#if (all(x > y for x, y in zip(face_tuple1, zone_tuple1)))==True and (all(x < y for x, y in zip(face_tuple2, zone_tuple2)))==True:
# radius=2
with graph.as_default():
dist = []
for i in range(len(embeddings)):
dist.append(distance.euclidean(l2_normalize(model.predict(prewhiten(
cv2.resize(frame[y_face:y_face + h_face, x_face:x_face + w_face], (160, 160)).reshape(
-1, 160,
160,
3)))),
embeddings[i].reshape(1, 128)))
dist = np.array(dist)
if os.path.exists(only_date + '.txt') == False:
f = open(only_date + '.txt', "a+")
log_in.clear()
log_out.clear()
else:
if dist.min() > 1.20:
log = 'Unauthorized Entry'
emp_id = 'None'
f1 = open("unauthorised.txt", "a")
f1.writelines(f"\n{cam_id},{time_date},{log}")
elif dist.min() <= 1:
emp_id = names[dist.argmin()]
if int(emp_id) not in log_in and curr_time >= login_time:
log = 'punch-in'
f2 = open(only_date + '.txt', "a")
f2.writelines(f"\n{cam_id},{emp_id},{time_date},{log}")
f2.close()
log_in.append(int(emp_id))
print(log_in)
if int(emp_id) in log_in and curr_time >= logout_time and int(emp_id) not in log_out:
# and center[0] > 750 and center[0] > 960:
log = 'punch-out'
f2 = open(only_date + '.txt', "a")
f2.writelines(f"\n{cam_id},{emp_id},{time_date},{log}")
f2.close()
log_out.append(int(emp_id))
else:
emp_id = 'None'
log = 'unidentified'
if emp_id != 'unauthorized' and emp_id != 'unidentified':
font_size = int(
slope[dist.argmin()] * ((w_face + 2 * dw) // 3) * 2 + intercept[dist.argmin()])
color = (0, 255, 0)
elif emp_id == 'unauthorized':
font_size = int(
slope[dist.argmin()] * ((w_face + 2 * dw) // 3) * 2 + intercept[dist.argmin()])
color = (0, 0, 255)
else:
font_size = int(0.1974311 * ((w_face + 2 * dw) // 3) * 2 + 0.03397702412218706)
color = (0, 255, 0)
font = ImageFont.truetype(font_path, font_size)
size = font.getbbox(emp_id)
cv2.rectangle(frame,
pt1=(x_face - int(np.floor(dw)), (y_face - int(np.floor(dh)))),
pt2=(
(x_face + w_face + int(np.ceil(dw))), (y_face + h_face + int(np.ceil(dh)))),
color=(0, 255, 0),
thickness=2) # Face Rectangle
cv2.rectangle(frame,
pt1=(x_face - int(np.floor(dw)), y_face - int(np.floor(dh)) - size[1]),
pt2=(x_face + size[0], y_face - int(np.floor(dh))),
color=(0, 255, 0),
thickness=-1)
img = Image.fromarray(frame)
draw = ImageDraw.Draw(img)
draw.text((x_face - int(np.floor(dw)), y_face - int(np.floor(dh)) - size[1]), emp_id,
font=font,
fill=color)
frame = np.array(img)
if emp_id == 'unauthorized':
frame_name = f'{emp_id}_{frame_number}.jpg'
cv2.imwrite(f'data/unauthorized_faces/{frame_name}',
cv2.resize(frame[y_face:y_face + h_face, x_face:x_face + w_face],
(250, 250)))
elif emp_id != 'unauthorised' and emp_id != 'unidentified':
frame_name = f'{emp_id}_{frame_number}.jpg'
cv2.imwrite(f'data/detected_faces/{frame_name}',
cv2.resize(frame[y_face:y_face + h_face, x_face:x_face + w_face],
(250, 250)))
# add_attachment(frame_name)
frame_number += 1
end = time.time()
print(end-start)
print(emp_id)
if log != 'unidentified':
data = {'emp_id': emp_id, 'date': time_date, 'log': log}
yield json.dumps(data) + "\n"
# cv2.imshow('Frame', cv2.resize(frame, (950, 950)))
if cv2.waitKey(15) & 255 == ord('q'):
break
else:
continue
return Response(inner(), mimetype='application/json')
if __name__=='__main__':
app.run(host="0.0.0.0",threaded=True)
This is my face recognition model integrated in flask.
https://github.com/safwankdb/Neural-Style-Transfer
I created a model file by running the code in the link above. I then converted it to ONNX to run with C++ and OpenCV.
But while Input (1,3,512,512) in the ONNX file I exported, it is in the form (1,512,28,28) in Output. My export code:
VGG.eval()
torch.save(VGG, 'torchmodel.pth')
dummy_input = Variable(torch.randn(1, 3, 512, 512, device='cuda:1'))
input_names = ['input']
output_names = ['output']
onnxfile='style.onnx'
torch.onnx.export(VGG,dummy_input,onnxfile,verbose=False,input_names=input_names,opset_version=11, output_names=output_names)
I tried export parameters from some sites but it didn't work.
Does anyone have an opinion on this matter?
Original code :
# -*- coding: utf-8 -*-
"""StyleTransfer.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/15JKaqmpVNr8NhURJWgbkvIl1sd0aKS3o
"""
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision as tv
from PIL import Image
import imageio
import numpy as np
from matplotlib import pyplot as plt
to_tensor = tv.transforms.Compose([
tv.transforms.Resize((512,512)),
tv.transforms.ToTensor(),
tv.transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[1, 1, 1]),
])
unload = tv.transforms.Compose([
tv.transforms.Normalize(mean=[-0.485,-0.456,-0.406],
std=[1,1,1]),
tv.transforms.Lambda(lambda x: x.clamp(0,1))
])
to_image = tv.transforms.ToPILImage()
style_img = 'udnie.jpg'
input_img = 'chicago.jpg'
style_img = Image.open(style_img)
input_img = Image.open(input_img)
style_img = to_tensor(style_img).cuda()
input_img = to_tensor(input_img).cuda()
def get_features(module, x, y):
# print('here')
features.append(y)
def gram_matrix(x):
b, c, h, w = x.size()
F = x.view(b,c,h*w)
G = torch.bmm(F, F.transpose(1,2))/(h*w)
return G
VGG = tv.models.vgg19(pretrained=True).features
VGG.cuda()
for i, layer in enumerate(VGG):
if i in [0,5,10,19,21,28]:
VGG[i].register_forward_hook(get_features)
elif isinstance(layer, nn.MaxPool2d):
VGG[i] = nn.AvgPool2d(kernel_size=2)
VGG.eval()
for p in VGG.parameters():
p.requires_grad = False
features = []
VGG(input_img.unsqueeze(0))
c_target = features[4].detach()
features = []
VGG(style_img.unsqueeze(0))
f_targets = features[:4]+features[5:]
gram_targets = [gram_matrix(i).detach() for i in f_targets]
alpha = 1
beta = 1e3
iterations = 200
image = input_img.clone().unsqueeze(0)
# image = torch.randn(1,3,512,512).cuda()
images = []
optimizer = optim.LBFGS([
image.requires_grad_()], lr=1)
mse_loss = nn.MSELoss(reduction='mean')
l_c = []
l_s = []
counter = 0
for itr in range(iterations):
features = []
def closure():
optimizer.zero_grad()
VGG(image)
t_features = features[-6:]
content = t_features[4]
style_features = t_features[:4]+t_features[5:]
t_features = []
gram_styles = [gram_matrix(i) for i in style_features]
c_loss = alpha * mse_loss(content, c_target)
s_loss = 0
for i in range(5):
n_c = gram_styles[i].shape[0]
s_loss += beta * mse_loss(gram_styles[i],gram_targets[i])/(n_c**2)
total_loss = c_loss+s_loss
l_c.append(c_loss)
l_s.append(s_loss)
total_loss.backward()
return total_loss
optimizer.step(closure)
print('Step {}: S_loss: {:.8f} C_loss: {:.8f}'.format(itr, l_s[-1], l_c[-1]))
if itr%1 == 0:
temp = unload(image[0].cpu().detach())
temp = to_image(temp)
temp = np.array(temp)
images.append(temp)
imageio.mimsave('progress.gif', images)
plt.clf()
plt.plot(l_c, label='Content Loss')
plt.legend()
plt.savefig('loss1.png')
plt.clf()
plt.plot(l_s, label='Style Loss')
plt.legend()
plt.savefig('loss2.png')
plt.imsave('last.jpg',images[-1])
I am trying to deploy the low light image enhancement to the web using Flask. The main thing that I want to do is, run a webpage on localhost, and when I upload a low light image the model should enhance the image and show the result on same webpage.
Here is my test.py codes:
from glob import glob
import numpy as np
import scipy
import keras
import os
import Network
import utls
import time
import cv2
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--input", "-i", type=str, default='../input', help='test image folder')
parser.add_argument("--result", "-r", type=str, default='../result', help='result folder')
parser.add_argument("--model", "-m", type=str, default='Syn_img_lowlight_withnoise', help='model name')
parser.add_argument("--com", "-c", type=int, default=1, help='output with/without origional image and mid result')
parser.add_argument("--highpercent", "-hp", type=int, default=95, help='should be in [85,100], linear amplification')
parser.add_argument("--lowpercent", "-lp", type=int, default=5, help='should be in [0,15], rescale the range [p%,1] to [0, 1]')
parser.add_argument("--gamma", "-g", type=int, default=8, help='should be in [6,10], increase the saturability')
parser.add_argument("--maxrange", "-mr", type=int, default=8, help='linear amplification range')
arg = parser.parse_args()
result_folder = arg.result
if not os.path.isdir(result_folder):
os.makedirs(result_folder)
input_folder = arg.input
path = glob(input_folder+'/*.*')
model_name = arg.model
mbllen = Network.build_mbllen((None, None, 3))
mbllen.load_weights('../models/'+model_name+'.h5')
opt = keras.optimizers.Adam(lr=2 * 1e-04, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
mbllen.compile(loss='mse', optimizer=opt)
flag = arg.com
lowpercent = arg.lowpercent
highpercent = arg.highpercent
maxrange = arg.maxrange/10.
hsvgamma = arg.gamma/10.
for i in range(len(path)):
img_A_path = path[i]
img_A = utls.imread_color(img_A_path)
img_A = img_A[np.newaxis, :]
starttime = time.process_time()
out_pred = mbllen.predict(img_A)
endtime = time.process_time()
print('The ' + str(i+1)+'th image\'s Time:' +str(endtime-starttime)+'s.')
fake_B = out_pred[0, :, :, :3]
fake_B_o = fake_B
gray_fake_B = fake_B[:, :, 0] * 0.299 + fake_B[:, :, 1] * 0.587 + fake_B[:, :, 1] * 0.114
percent_max = sum(sum(gray_fake_B >= maxrange))/sum(sum(gray_fake_B <= 1.0))
# print(percent_max)
max_value = np.percentile(gray_fake_B[:], highpercent)
if percent_max < (100-highpercent)/100.:
scale = maxrange / max_value
fake_B = fake_B * scale
fake_B = np.minimum(fake_B, 1.0)
gray_fake_B = fake_B[:,:,0]*0.299 + fake_B[:,:,1]*0.587 + fake_B[:,:,1]*0.114
sub_value = np.percentile(gray_fake_B[:], lowpercent)
fake_B = (fake_B - sub_value)*(1./(1-sub_value))
imgHSV = cv2.cvtColor(fake_B, cv2.COLOR_RGB2HSV)
H, S, V = cv2.split(imgHSV)
S = np.power(S, hsvgamma)
imgHSV = cv2.merge([H, S, V])
fake_B = cv2.cvtColor(imgHSV, cv2.COLOR_HSV2RGB)
fake_B = np.minimum(fake_B, 1.0)
if flag:
outputs = np.concatenate([img_A[0,:,:,:], fake_B_o, fake_B], axis=1)
else:
outputs = fake_B
filename = os.path.basename(path[i])
img_name = result_folder+'/' + filename
# scipy.misc.toimage(outputs * 255, high=255, low=0, cmin=0, cmax=255).save(img_name)
outputs = np.minimum(outputs, 1.0)
outputs = np.maximum(outputs, 0.0)
utls.imwrite(img_name, outputs)
The data_load.py codes:
from glob import glob
import numpy as np
import random
import scipy
import os
import cv2 as cv
class Dataloader():
def __init__(self, dataset_name, crop_shape=(256, 256)):
self.dataset_name = dataset_name
self.crop_shape = crop_shape
def imread_color(self, path):
img = cv.imread(path, cv.IMREAD_COLOR | cv.IMREAD_ANYDEPTH)/255.
b, g, r = cv.split(img)
img_rgb = cv.merge([r, g, b])
return img_rgb
def imwrite(self, path, img):
r, g, b = cv.split(img)
img_rgb = cv.merge([b, g, r])
cv.imwrite(path, img_rgb)
def load_data(self, batch_size=16):
path = glob('../dataset/train/*.jpg')
self.n_batches = int(len(path) / batch_size)
while 1:
random.shuffle(path)
for i in range(self.n_batches - 1):
batch_path = path[i * batch_size:(i + 1) * batch_size]
input_imgs = np.empty((batch_size, self.crop_shape[0], self.crop_shape[1], 6), dtype="float32")
gt = np.empty((batch_size, self.crop_shape[0], self.crop_shape[1], 3), dtype="float32")
number = 0
for img_B_path in batch_path:
img_B = self.imread_color(img_B_path)
path_mid = os.path.split(img_B_path)
path_A_1 = path_mid[0] + '_' + self.dataset_name
path_A = os.path.join(path_A_1, path_mid[1])
img_A = self.imread_color(path_A)
nw = random.randint(0, img_B.shape[0] - self.crop_shape[0])
nh = random.randint(0, img_B.shape[1] - self.crop_shape[1])
crop_img_A = img_A[nw:nw + self.crop_shape[0], nh:nh + self.crop_shape[1], :]
crop_img_B = img_B[nw:nw + self.crop_shape[0], nh:nh + self.crop_shape[1], :]
if np.random.randint(2, size=1)[0] == 1: # random flip
crop_img_A = np.flipud(crop_img_A)
crop_img_B = np.flipud(crop_img_B)
if np.random.randint(2, size=1)[0] == 1:
crop_img_A = np.fliplr(crop_img_A)
crop_img_B = np.fliplr(crop_img_B)
if np.random.randint(2, size=1)[0] == 1: # random transpose
crop_img_A = np.transpose(crop_img_A, (1, 0, 2))
crop_img_B = np.transpose(crop_img_B, (1, 0, 2))
input_imgs[number, :, :, :] = np.concatenate([crop_img_A, crop_img_B], axis=-1)
gt[number, :, :, :] = crop_img_B
number += 1
yield input_imgs, gt
The utls.py codes:
import tensorflow as tf
from keras import backend as K
import numpy as np
import scipy
import os
import cv2 as cv
def bright_mae(y_true, y_pred):
return K.mean(K.abs(y_pred[:,:,:,:3] - y_true[:,:,:,:3]))
def bright_mse(y_true, y_pred):
return K.mean((y_pred[:,:,:,:3] - y_true[:,:,:,:3])**2)
def bright_AB(y_true, y_pred):
return K.abs(K.mean(y_true[:,:,:,:3])-K.mean(y_pred[:,:,:,:3]))
def log10(x):
numerator = K.log(x)
denominator = K.log(K.constant(10, dtype=numerator.dtype))
return numerator / denominator
def bright_psnr(y_true, y_pred):
mse = K.mean((K.abs(y_pred[:,:,:,:3] - y_true[:,:,:,:3])) ** 2)
max_num = 1.0
psnr = 10 * log10(max_num ** 2 / mse)
return psnr
def _tf_fspecial_gauss(size, sigma):
"""Function to mimic the 'fspecial' gaussian MATLAB function
"""
x_data, y_data = np.mgrid[-size//2 + 1:size//2 + 1, -size//2 + 1:size//2 + 1]
x_data = np.expand_dims(x_data, axis=-1)
x_data = np.expand_dims(x_data, axis=-1)
y_data = np.expand_dims(y_data, axis=-1)
y_data = np.expand_dims(y_data, axis=-1)
x = tf.constant(x_data, dtype=tf.float32)
y = tf.constant(y_data, dtype=tf.float32)
g = tf.exp(-((x**2 + y**2)/(2.0*sigma**2)))
return g / tf.reduce_sum(g)
def tf_ssim(img1, img2, cs_map=False, mean_metric=True, size=11, sigma=1.5):
window = _tf_fspecial_gauss(size, sigma) # window shape [size, size]
K1 = 0.01
K2 = 0.03
L = 1 # depth of image (255 in case the image has a differnt scale)
C1 = (K1*L)**2
C2 = (K2*L)**2
mu1 = tf.nn.conv2d(img1, window, strides=[1,1,1,1], padding='VALID')
mu2 = tf.nn.conv2d(img2, window, strides=[1,1,1,1],padding='VALID')
mu1_sq = mu1*mu1
mu2_sq = mu2*mu2
mu1_mu2 = mu1*mu2
sigma1_sq = tf.nn.conv2d(img1*img1, window, strides=[1,1,1,1],padding='VALID') - mu1_sq
sigma2_sq = tf.nn.conv2d(img2*img2, window, strides=[1,1,1,1],padding='VALID') - mu2_sq
sigma12 = tf.nn.conv2d(img1*img2, window, strides=[1,1,1,1],padding='VALID') - mu1_mu2
if cs_map:
value = (((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*
(sigma1_sq + sigma2_sq + C2)),
(2.0*sigma12 + C2)/(sigma1_sq + sigma2_sq + C2))
else:
value = ((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*
(sigma1_sq + sigma2_sq + C2))
if mean_metric:
value = tf.reduce_mean(value)
return value
def tf_ms_ssim(img1, img2, mean_metric=True, level=5):
weight = tf.constant([0.0448, 0.2856, 0.3001, 0.2363, 0.1333], dtype=tf.float32)
mssim = []
mcs = []
for l in range(level):
ssim_map, cs_map = tf_ssim(img1, img2, cs_map=True, mean_metric=False)
mssim.append(tf.reduce_mean(ssim_map))
mcs.append(tf.reduce_mean(cs_map))
filtered_im1 = tf.nn.avg_pool(img1, [1,2,2,1], [1,2,2,1], padding='SAME')
filtered_im2 = tf.nn.avg_pool(img2, [1,2,2,1], [1,2,2,1], padding='SAME')
img1 = filtered_im1
img2 = filtered_im2
# list to tensor of dim D+1
mssim = tf.stack(mssim, axis=0)
mcs = tf.stack(mcs, axis=0)
value = (tf.reduce_prod(mcs[0:level-1]**weight[0:level-1])*
(mssim[level-1]**weight[level-1]))
if mean_metric:
value = tf.reduce_mean(value)
return value
def bright_SSIM(y_true, y_pred):
SSIM_loss = tf_ssim(tf.expand_dims(y_pred[:,:,:,0], -1), tf.expand_dims(y_true[:,:,:,0], -1))+tf_ssim(tf.expand_dims(y_pred[:,:,:,1], -1), tf.expand_dims(y_true[:,:,:,1], -1)) + tf_ssim(tf.expand_dims(y_pred[:,:,:,2], -1), tf.expand_dims(y_true[:,:,:,2], -1))
return SSIM_loss/3
def psnr_cau(y_true, y_pred):
mse = np.mean((np.abs(y_pred - y_true)) ** 2)
max_num = 1.0
psnr = 10 * np.log10(max_num ** 2 / mse)
return psnr
def save_model(model, name, epoch, batch_i):
modelname = './Res_models/' + str(epoch) + '_' + str(batch_i) + name + '.h5'
model.save_weights(modelname)
def imread_color(path):
img = cv.imread(path, cv.IMREAD_COLOR | cv.IMREAD_ANYDEPTH) / 255.
b, g, r = cv.split(img)
img_rgb = cv.merge([r, g, b])
return img_rgb
# return scipy.misc.imread(path, mode='RGB').astype(np.float) / 255.
def imwrite(path, img):
r, g, b = cv.split(img*255)
img_rgb = cv.merge([b, g, r])
cv.imwrite(path, img_rgb)
# scipy.misc.toimage(img * 255, high=255, low=0, cmin=0, cmax=255).save(path)
def range_scale(x):
return x * 2 - 1.
In the below codes, I wanted to make a webpage and create an upload button to take low light images from users to enhance it by using the model. But I could not deploy the model as well.
main.py:
from flask import Flask, request
from werkzeug.utils import secure_filename
import torch
def update_image():
app = Flask(
__name__,
static_url_path='',
static_folder=''
)
def get_form():
return """
<form action="/" enctype="multipart/form-data" method="POST">
<input type="file" id="file" name="file">
<input type="submit">
</form>
"""
#app.route("/", methods = ['GET', 'POST'])
def hello_world():
if request.method == 'GET':
return get_form()
elif request.method == 'POST':
f = request.files['file']
print(f.filename)
f.save(secure_filename(f.filename))
update_image(f.filename)
return f'{get_form()} <img src="{f.filename}"/>'
I'm trying to hide text inside an image and using PIL to save the encoded image from the neural network with embeds the data into the image.
https://futureboy.us/stegano/compinput.html
I'm using this site to compare difference between the original image and the one which is encoded.
When I used opencv2 to save the image and compared the output on the website I mentioned it shows very less difference but when I used PIL to save the image it results in image which is very dim and comparing the original image and output shows all the pixel values have been changed.
import numpy as np
from tensorflow.keras.layers import *
from tensorflow.keras.losses import *
from tensorflow.keras.callbacks import *
from tensorflow.keras.models import *
from tensorflow.keras.metrics import *
from tensorflow.keras.preprocessing.image import img_to_array, load_img, array_to_img
import cv2
import matplotlib
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
from PIL import Image
def rand_img(size):
return np.random.randint(0, 256, size) / 255.0
def rand_sentence(len, max):
return np.random.randint(0, max, len)
def onehot(sentece, max):
onehot = np.zeros((len(sentece), max))
for i, v in enumerate(sentece):
onehot[i, v] = 1
return onehot
def data_generator(image_size, sentence_len, sentence_max_word, batch_size=32):
while True:
x_img = np.zeros((batch_size, image_size[0], image_size[1], image_size[2]))
x_sen = np.zeros((batch_size, sentence_len))
y_img = np.zeros((batch_size, image_size[0], image_size[1], image_size[2]))
y_sen = np.zeros((batch_size, sentence_len, sentence_max_word))
for i in range(batch_size):
img = rand_img(image_size)
sentence = rand_sentence(sentence_len, sentence_max_word)
sentence_onehot = onehot(sentence, sentence_max_word)
x_img[i] = img
x_sen[i] = sentence
y_img[i] = img
y_sen[i] = sentence_onehot
yield [x_img, x_sen], [y_img, y_sen]
def get_model(image_shape, sentence_len, max_word):
input_img = Input(image_shape)
input_sen = Input((sentence_len,))
embed_sen = Embedding(max_word, 100)(input_sen)
flat_emb_sen = Flatten()(embed_sen)
flat_emb_sen = Reshape((image_shape[0], image_shape[1], 1))(flat_emb_sen)
trans_input_img = Conv2D(20, 1, activation="relu")(input_img)
enc_input = Concatenate(axis=-1)([flat_emb_sen, trans_input_img])
out_img = Conv2D(3, 1, activation='relu', name='image_reconstruction')(enc_input)
decoder_model = Sequential(name="sentence_reconstruction")
decoder_model.add(Conv2D(1, 1, input_shape=(100, 100, 3)))
decoder_model.add(Reshape((sentence_len, 100)))
decoder_model.add(TimeDistributed(Dense(max_word, activation="softmax")))
out_sen = decoder_model(out_img)
model = Model(inputs=[input_img, input_sen], outputs=[out_img,out_sen])
model.compile('adam', loss=[mean_absolute_error, categorical_crossentropy],
metrics={'sentence_reconstruction': categorical_accuracy})
encoder_model = Model(inputs=[input_img, input_sen], outputs=[out_img])
#return model, encoder_model, decoder_model
return model, encoder_model, decoder_model
def ascii_encode(message, sentence_len):
sen = np.zeros((1, sentence_len))
for i, a in enumerate(message.encode("ascii")):
sen[0, i] = a
return sen
def ascii_decode(message):
return ''.join(chr(int(a)) for a in message[0].argmax(-1))
def main():
image_shape = (100, 100, 3)
sentence_len = 100
max_word = 256
gen = data_generator(image_shape, sentence_len, max_word, 64)
model, encoder, decoder = get_model(image_shape, sentence_len, max_word)
try:
model.load_weights("best_weights.h5")
except:
model.fit(gen, epochs=512, steps_per_epoch=400, callbacks=[
ModelCheckpoint("best_weights.h5",monitor="loss",verbose=1,save_weights_only=True,save_best_only=True)])
print(model.summary())
img = np.expand_dims(img_to_array(load_img("img.jpg")) / 255.0, axis=0)
x = input("Enter sentence:\n")
sen = ascii_encode(x, sentence_len)
y = encoder.predict([img, sen])
x = np.reshape(y,(100,100,3))
#img = cv2.cvtColor(x, cv2.COLOR_BGR2RGB)
img = x
'''out = array_to_img(x)
out.save("out.jp2")'''
cv2.imshow("img",img)
#rescaled = (255.0 / img.max() * (img - img.min())).astype(np.uint8)
rescaled = (img/img.max()*255).astype(np.uint8)
im = Image.fromarray(rescaled)
im.save('out.png')
cv2.waitKey(0)
y = np.expand_dims(img_to_array(load_img("out.png")) / 255.0, axis=0)
y_hat = decoder.predict(y)
print(ascii_decode(y_hat))
if __name__ == "__main__":
main()
The following code
from torch.utils.data import DataLoader
from defect_segmentation.data_loading.DatasetSingleImage import dataset_single_image_default
import numpy as np
if __name__ == "__main__":
import matplotlib.pyplot as plt
def main():
dataset = dataset_single_image_default()
batch_size = 16
shuffle = True
num_workers = 0
loader = DataLoader(dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers)
for i_batch, sample_batched in enumerate(loader):
fig, axs = plt.subplots(int(np.sqrt(batch_size)), batch_size // int(np.sqrt(batch_size)))
fig.suptitle(f"i_batch = {i_batch}")
for i_sample, ax in zip(range(sample_batched.shape[0]), axs.flat):
ax.set_title(f"Sample #{i_sample}")
ax.axis("off")
ax.imshow(sample_batched[i_sample, :, :])
plt.pause(0.001)
main()
works and outputs figures like so
This is fine.
Problem is by the 10th figure, it becomes very slow to populate the figures. I don't know what can be causing this.
For completeness, here is the code for creating the dataset (striding over a single image):
from torch.utils.data import Dataset
from Utils.ConfigProvider import ConfigProvider
import cv2
import os
from overrides import overrides # pip install overrides
class DatasetSingleImage(Dataset):
def __init__(self, image_path: str, sample_shape: tuple, strides: tuple):
self._path = image_path
assert os.path.isfile(self._path)
self._im = cv2.imread(self._path)
self._shape = self._im.shape
self._rows, self._cols = self._shape[0], self._shape[1]
self._sample_shape = sample_shape
self._sample_rows, self._sample_cols = self._sample_shape[0], self._sample_shape[1]
self._strides = strides
self._stride_rows, self._stride_cols = self._strides[0], self._strides[1]
# self._rows_start_range = range(0, self._rows, self._stride_rows)
# self._cols_start_range = range(0, self._cols, self._stride_cols)
self._rows_tuples_range = \
[(c, min(c + self._sample_rows, self._rows)) for c in range(0, self._rows - self._sample_rows, self._stride_rows)]
self._cols_tuples_range = \
[(r, min(r + self._sample_cols, self._cols)) for r in range(0, self._cols - self._sample_cols, self._stride_cols)]
self._n_strides_rows = len(self._rows_tuples_range)
self._n_strides_cols = len(self._cols_tuples_range)
self._total_strides = self._n_strides_rows * self._n_strides_cols
def __len__(self):
return self._total_strides
#overrides # pip install overrides
def __getitem__(self, ind):
row_ind = ind // self._n_strides_cols
col_ind = ind % self._n_strides_cols
sample_x = self._rows_tuples_range[row_ind]
sample_y = self._cols_tuples_range[col_ind]
sample = self._im[sample_x[0]:sample_x[1], sample_y[0]:sample_y[1]]
assert sample.shape[:2] == self._sample_shape
return sample
def dataset_single_image_default():
path = ConfigProvider.config().data.defective_inspected_path1
sample_shape = (50, 50)
strides = (25, 25)
dataset = DatasetSingleImage(path, sample_shape, strides)
return dataset
What is making the plotting slow, and how to fix it?