OpenCV and Tensorflow Colour issue (Most probably due to channels maybe?) - tensorflow

I was using this pytorch model for real-ESRGAN. It was giving good results:
Now I converted this model to tensorflow one. Now when I use this, I am getting image super resoluted (I reached to that conclusion due to image size of output) but its color channels are in very weird condition:
I am using opencv to take input and then model to process it. I feel issue is with BGR and RGB of OpenCV and Tensorflow but using cv2.COLOR_BGRTORGB not helping.
Any idea how to solve this?
This is my code:
from pyexpat import model
import tensorflow as tf
import os.path as osp
import glob
import cv2
import numpy as np
import torch
test_img_folder = './images/*'
model = tf.saved_model.load("./RealESRGAN_1/")
idx = 0
for path in glob.glob(test_img_folder):
idx += 1
base = osp.splitext(osp.basename(path))[0]
print(idx, base)
# read images
img = cv2.imread(path, cv2.IMREAD_COLOR)
print(img.shape)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow('Input',img)
cv2.waitKey(0)
img = np.transpose(img, (2,0,1))
img = np.expand_dims(img, axis=0)
img = tf.dtypes.cast(img, tf.float32)
with torch.no_grad():
output = model(x = img)
output = output['sum'].numpy()
output = output[0, :, :, :]
output = np.transpose(output, (1,2,0))
cv2.imwrite('./results/{:s}_rlt.png'.format(base), output)

Related

Testing image classification model on new images

I am still a bit new to deep learning.
def predictionrelease(preds):
arr=[]
for i in range(0,len(preds)):
ans=np.argmax(preds[i])
arr.append(ans)
len(arr)
return arr
dir_path = 'predict'
for i in os.listdir(dir_path):
img = image.load_img(dir_path+ '\\' + i, target_size = (200,200,3))
plt.imshow(img)
plt.show()
X = image.img_to_array(img)
X = np.expand_dims(X, axis = 0)
images = np.vstack([X])
val = predictionrelease(model.predict(images))
print(val)
I was able to train a model on image classification. Now i try to predict new images in a single file using the model, but it's end up predicting only one of the images, whereas i want it to give prediction for all the of the images in the file. I iterated over the images, but it's seems not to be working. There is the code:
As a workaround you can use the code below to predict on all images present in a folder.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
dir_path = path_to_the_folder
images=[]
for i in os.listdir(dir_path):
img = tf.keras.utils.load_img(dir_path + i, target_size = (200,200,3))
plt.imshow(img)
plt.show()
X = tf.keras.utils.img_to_array(img)
X = np.expand_dims(X, axis = 0)
images.append(X)
arr=[]
pred=[]
for i in range(len(images)):
pred.append(model.predict(images[i],))
ans=np.argmax(pred)
arr.append(ans)
print(arr)

Different results in tensorflow prediction

I cannot understand why the following codes gives different results. I'm printing the first 3 components of the prediction array to compare results. my_features and feat have totally different results, but they should be the same, given the model and the data are the same. There should be something wrong in the loading and image preprocessing, but I cannot find it. Any help will be appreciated.
import tensorflow as tf
import os
import numpy as np
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications import MobileNetV3Small
from tensorflow.keras.applications.imagenet_utils import preprocess_input
model= MobileNetV3Small(weights='imagenet', include_top=False, pooling='avg')
DatasetPath= "DB"
imagePathList= sorted(os.listdir(DatasetPath))
imagePathList= [os.path.join(DatasetPath, imagePath) for imagePath in imagePathList]
def read_image(filename):
image_string = tf.io.read_file(filename)
image = tf.image.decode_jpeg(image_string, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.resize(image, [224,224])
image = tf.keras.applications.mobilenet_v3.preprocess_input(image)
return image
ds_imagePathList= tf.data.Dataset.from_tensor_slices(imagePathList)
dataset = ds_imagePathList.map(read_image, num_parallel_calls=tf.data.AUTOTUNE)
dataset = dataset.batch(32, drop_remainder=False)
dataset = dataset.prefetch(tf.data.AUTOTUNE)
my_features = model.predict(dataset)
my_features[0][:3]
Second snippet
def loadProcessedImage(path):
#img = image.load_img(path, target_size=model.input_shape[1:3])
img = image.load_img(path, target_size= (224,224,3))
imgP = image.img_to_array(img)
imgP = np.expand_dims(imgP, axis=0)
imgP = preprocess_input(imgP)
return img, imgP
img, x = loadProcessedImage(imagePathList[0])
feat = model.predict(x)
feat = feat.flatten()
feat[:3]
The problem is related to the image resize. In the second snippet there is a call to load_img which internally uses pillow to load and resize the image. The problem is that tf.image.resize is not correct see here, and even this a 2018 blog post, the problem is still there

How to get the bounding box coordinates for the detected object

I am new to tensorflow.am very confusing to get the bounding box coordinates for the detected object.how to get the bounding box to the detected object.this is my code please help!!
from PIL import Image
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import os
import cv2
test_dir=os.getcwd()+'/test_img'
test_img=os.listdir(test_dir)
def convert_to_array(img):
im = cv2.imread(img)
img_ = Image.fromarray(im, 'RGB')
image = img_.resize((224,224))
return np.array(image)
def get_cell_name(label):
if label==0:
return "daisy"
if label==1:
return "dandelion"
model = tf.keras.experimental.load_from_saved_model('E:/model/flowers.h5', custom_objects={'KerasLayer':hub.KerasLayer})
model.build((None, 224, 224, 3))
for img in test_img:
file = os.path.join(test_dir, img)
ar=convert_to_array(file)
ar=ar/255
label=1
a=[]
a.append(ar)
a=np.array(a)
score=model.predict(a,verbose=1)
print(score)
label_index=np.argmax(score)
print(label_index)
acc=np.max(score)
Cell=get_cell_name(label_index)
print(Cell,acc)
img = cv2.imread(file)
text='this is '+Cell
cv2.putText(img,text, (5,15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 255)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
is you asking about this structure.
model = tf.keras.Sequential([
hub.KerasLayer(MODULE_HANDLE, output_shape=[FV_SIZE],
trainable=do_fine_tuning),
tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(train_generator.num_classes, activation='softmax',
kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
model.build((None,)+IMAGE_SIZE+(3,))
model.summary()```
You are trying to use a classification network. A classification network will classify an entire image into one of the classes.
For detecting objects in image, try networks such as RCNN, RetinaNet, Yolo etc.

How to import a model using pb file in tensorflow

I have been assigned a task to fine tune deeplab V3+ using tensorflow and python. For that purpose I download the frozen model from deeplab github page. !
I downloaded this file.
Then I searched through the web on how to create a model using these files
There are method only to create model using .ckpt files and .meta files but i don't have any of those file
There are only methods available to create graph from the .pb file. I don't know what to do after creating a graph using the .pb file. I to import the frozen model using these files. Thank you in advance
This should work
import os
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
from PIL import Image
import tensorflow as tf
INPUT_TENSOR_NAME = 'ImageTensor:0'
OUTPUT_TENSOR_NAME = 'SemanticPredictions:0'
INPUT_SIZE = 513
with tf.gfile.FastGFile('model/frozen_inference_graph.pb', "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
g_in = tf.import_graph_def(graph_def, name="")
sess = tf.Session(graph=g_in)
def run(image):
width, height = image.size
resize_ratio = 1.0 * INPUT_SIZE / max(width, height)
target_size = (int(resize_ratio * width), int(resize_ratio * height))
resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)
batch_seg_map = sess.run(
OUTPUT_TENSOR_NAME,
feed_dict={INPUT_TENSOR_NAME: [np.asarray(resized_image)]})
seg_map = batch_seg_map[0]
return resized_image, seg_map
input_image = Image.open('test.jpg')
resized_im, seg_map = run(input_image)
fig = plt.figure()
fig.add_subplot(1, 2, 1)
plt.imshow(resized_im)
fig.add_subplot(1, 2, 2)
plt.imshow(np.ma.masked_equal(seg_map, 0))

How to multiply input images with mask in tensorflow?

I want to multiply every input image with a mask of the same size as the input image. How would I do that in tensorflow?
My image reading function looks like this so far:
img_contents = tf.read_file(input_queue[0])
label_contents = tf.read_file(input_queue[1])
img = tf.image.decode_png(img_contents, channels=3)
label = tf.image.decode_png(label_contents, channels=1)
# Now I want to do something like this?
mask = tf.constant(1.0, dtype=tf.float32, shape=img.shape)
img_masked = tf.multiply(img,mask)
Is that possible?
Not sure if img is already a tensor object and I can use that function here. I'm new to tensorflow...
Here is the code that works well for me. I'm using jupyter notebook to run the code.
%matplotlib inline
import tensorflow as tf
from matplotlib.image import imread
import matplotlib.pyplot as plt
# Loading test image from the local filesystem
x = tf.Variable(imread("test_img.jpg"),dtype='float32')
x_mask = tf.Variable(imread("test_mask.jpg"),dtype='float32')
img_mult = tf.multiply(x,x_mask)
plt.imshow(imread("test_img.jpg"))
plt.show()
plt.imshow(imread("test_mask.jpg"))
plt.show()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
res = sess.run(img_mult)
plt.imshow(res)
plt.show()
Also, Here is a good YouTube tutorial covering image manipulation with TF: https://www.youtube.com/watch?v=bvHgESVuS6Q&t=447s