Show image mask tensor model - python

I am working on a tensor model.
I have trained it and now testing and evaluating it.
Img_score with the prediction is working correctly.
I want also to print the seg_mask image (seg_out in the code), but plt.imshow does not work, even if I permute it.
Moreover if I print the values of the seg_out image they are all negatives which I don't understand how to handle them in images.
Here is what I am using.
INPUT_WIDTH = 232
INPUT_HEIGHT = 640
INPUT_CHANNELS = 3
device = "cpu"
model = SegDecNet(device, INPUT_WIDTH, INPUT_HEIGHT, INPUT_CHANNELS)
model.set_gradient_multipliers(0)
model_path = "/final_state_dict.pth"
model.load_state_dict(torch.load(model_path, map_location=device))
# %%
img_path = '/20118.png'
img = cv2.imread(img_path) if INPUT_CHANNELS == 3 else cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (INPUT_WIDTH, INPUT_HEIGHT))
print(img.shape)
img = np.transpose(img, (2, 0, 1)) if INPUT_CHANNELS == 3 else img[np.newaxis]
img_t = torch.from_numpy(img)[np.newaxis].float() / 445400 # must be [BATCH_SIZE x CHANNELS x HEIGHT x WIDTH]
dec_out, seg_out = model(img_t)
img_score = torch.sigmoid(dec_out)
print(img_score)
print(seg_out[0][0].shape)
print(seg_out[0][0].permute(0, 1))
#plt.imshow(seg_out[0].permute(2, 0, 1))
thanks

Related

Keras ResNet50 model for Melanoma prediction

Below is my professor's code to implement Keras ResNet50.
model = ResNet50(weights='imagenet', include_top=False, input_shape=(150, 150,3))
Y_train = []
X_train = []
label = 0
# 2 for loop, 1 for classes and 1 for images in the class
for i in os.listdir(train_path): #for class
img_path = train_path + "/" + i + "/"
img_num = 0
for file in os.listdir(img_path): # for images
img_path1 = img_path+"/"+file
img=cv2.imread(img_path1)
img=cv2.resize(img,(150,150))
img = image.img_to_array(img)
img = resnet50.preprocess_input(np.expand_dims(img.copy(), axis=0))
img = model.predict(img)
X_train.append(img.flatten())
Y_train.append(label)
label = label+1
Y_test = []
X_test = []
label = 0
for i in os.listdir(test_path):
path = test_path + "/" + i + "/"
img_num = 0
for file in os.listdir(img_path):
img_path1 = img_path+"/"+file
img=cv2.imread(img_path1)
img=cv2.resize(img,(150,150))
img = image.img_to_array(img)
img = resnet50.preprocess_input(np.expand_dims(img.copy(), axis=0))
img = model.predict(img)
X_test.append(img.flatten()/255)
Y_test.append(label)
label = label+1
model = linear_model.LogisticRegression().fit(X_train,Y_train)
pred = model.predict(X_test)
cm = confusion_matrix(Y_test,pred)
print((cm[0,0]+cm[1,1])/(sum(sum(cm))))
I asked my professor about why there are "double prediction", as in the line img = model.predict(img) and LogReg is implemented after that. He explained that img = model.predict(img) is to embed the image, not provide prediction. However when I looked up to online material they all said model.predict is to predict the image output.
So how should I understand the line model.predict(img) in this case?

Variational Auto Encoder weird mosaic reconstructed result

I used VAE and want to train a VAE network but somehow after 150 epochs the output is very weird, the loss value is converged to ~0.07. In my Dataloader the image was transferred from int 16 to Gray image and there is my code, and the input image is [batch_size, 5, 512, 512]( 5 gray image), I have no idea why the output looks like that.
class MyDataset(Dataset):
def __init__(self, transform):
data_path="/content/drive/MyDrive/sub_sample/imgs_train_data_0_5_5bands.npy"
# train_ = CustomImageDataset(data_path, transform=None)
imgs_test = np.load(data_path)
# x = torch.zeros(3, 244, 395, dtype = torch.uint8)
print(imgs_test.dtype)
img = imgs_test.astype('float32')
imgs_test = (img - np.min(img, axis = (0,1))) / np.ptp(img, axis=(0,1))
print(imgs_test.dtype)
print(imgs_test.max(),imgs_test.min())
self.training = imgs_test
# print(max(imgs_test[0]),min(imgs_test[0]))
self.img = torch.from_numpy(self.training)
# self.img = F.interpolate(self.img, size = (256,256))
print(imgs_test.shape)
# self.transform = transform
# print('****',self.subset.shape[0])
def __getitem__(self, index):
# img_resize = np.random.random((13,256,256))
img_resize = self.img[index, :, :, :] # 读取每一个npy的数据
# print((img_resize[0,:,:].max()))
return img_resize
def __len__(self):
# print('the shape of the total dataset',len(self.training))
return len(self.training)
The reconstructed result:
BTW, the loss function is F.mse_loss(input,reconstruncted image)
I would be very appreciate if anyone can give me some advice.

Python image reshape (1,28,28,1) is not work in image processing

I have been stuck with this problem for a very long time and I could not find the solution.
Problem: I have to do "reshape" to practice the artificial intelligence I trained, but I couldn't do it.
Normally it works when you take the same picture from the outside.
def load_image(filename):
img = load_img(filename, grayscale=True, target_size=(28, 28))
img = img_to_array(img)
img = img.reshape(1, 28, 28, 1)
img = img.astype('float32')
img = img / 255.0
return img
def run_example():
img = load_image('images/5.png')
model = load_model('final_model.h5')
digit = model.predict_classes(img)
print(digit[0])
run_example()
This code works, but when I want to get the numbers from a picture with a lot of numbers, I have to get the numbers from the inside.
I was able to get one of the numbers here and one of them is like this:
digits_with_zeros[0].shape
# output: (171, 171)
When I try to apply "reshape" to this series, I get an error like this.
img = digits_with_zeros[0].reshape(28,28)
img = img_to_array(img)
img = img.reshape(1, 28, 28, 1)
img = img.astype('float32')
img = img / 255.0
model = load_model('final_model.h5')
digit = model.predict_classes(img)
output:
----> 1 img = digits_with_zeros[0].reshape(-1,28,28,1)
2 img = img_to_array(img)
3 img = img.reshape(1, 28, 28, 1)
4 img = img.astype('float32')
5 img = img / 255.0
ValueError: cannot reshape array of size 29241 into shape (28,28,1)
img = digits_with_zeros[0]
img = cv2.resize(img, dsize=(28, 28))
img = np.array(img).reshape(-1, 28,28,1)
model = load_model('final_model.h5')
digit = model.predict_classes(img)

Tensorflow error. TypeError: Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn

I am trying to run this on Amazon Sagemaker but I am getting this error while when I try to run it on my local machine, it works very fine.
this is my code:
import tensorflow as tf
import IPython.display as display
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (12,12)
mpl.rcParams['axes.grid'] = False
import numpy as np
import PIL.Image
import time
import functools
def tensor_to_image(tensor):
tensor = tensor*255
tensor = np.array(tensor, dtype=np.uint8)
if np.ndim(tensor)>3:
assert tensor.shape[0] == 1
tensor = tensor[0]
return PIL.Image.fromarray(tensor)
content_path = tf.keras.utils.get_file('YellowLabradorLooking_nw4.jpg', 'https://example.com/IMG_20200216_163015.jpg')
style_path = tf.keras.utils.get_file('kandinsky3.jpg','https://example.com/download+(2).png')
def load_img(path_to_img):
max_dim = 512
img = tf.io.read_file(path_to_img)
img = tf.image.decode_image(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
shape = tf.cast(tf.shape(img)[:-1], tf.float32)
long_dim = max(shape)
scale = max_dim / long_dim
new_shape = tf.cast(shape * scale, tf.int32)
img = tf.image.resize(img, new_shape)
img = img[tf.newaxis, :]
return img
def imshow(image, title=None):
if len(image.shape) > 3:
image = tf.squeeze(image, axis=0)
plt.imshow(image)
if title:
plt.title(title)
content_image = load_img(content_path)
style_image = load_img(style_path)
plt.subplot(1, 2, 1)
imshow(content_image, 'Content Image')
plt.subplot(1, 2, 2)
imshow(style_image, 'Style Image')
import tensorflow_hub as hub
hub_module = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/1')
stylized_image = hub_module(tf.constant(content_image), tf.constant(style_image))[0]
tensor_to_image(stylized_image)
file_name = 'stylized-image5.png'
tensor_to_image(stylized_image).save(file_name)
This is the exact error I get:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-c47a4db4880c> in <module>()
53
54
---> 55 content_image = load_img(content_path)
56 style_image = load_img(style_path)
57
in load_img(path_to_img)
34
35 shape = tf.cast(tf.shape(img)[:-1], tf.float32)
---> 36 long_dim = max(shape)
37 scale = max_dim / long_dim
38
~/anaconda3/envs/amazonei_tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in iter(self)
475 if not context.executing_eagerly():
476 raise TypeError(
--> 477 "Tensor objects are only iterable when eager execution is "
478 "enabled. To iterate over this tensor use tf.map_fn.")
479 shape = self._shape_tuple()
TypeError: Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn.
Your error is being raised in this function load_img:
def load_img(path_to_img):
max_dim = 512
img = tf.io.read_file(path_to_img)
img = tf.image.decode_image(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
shape = tf.cast(tf.shape(img)[:-1], tf.float32)
long_dim = max(shape)
scale = max_dim / long_dim
new_shape = tf.cast(shape * scale, tf.int32)
img = tf.image.resize(img, new_shape)
img = img[tf.newaxis, :]
return img
Specifically, this line:
long_dim = max(shape)
You are passing a tensor to the built-in Python max function in graph execution mode. You can only iterate through tensors in eager-execution mode. You probably want to use tf.reduce_max instead:
long_dim = tf.reduce_max(shape)

what is meaning of error 'NoneType' object is not subscriptable

I am working on face recognition project , in which I train the model. prediction I load images and want to calculate distanse betwwen 2 images. while predection i am getting below error:
TypeError Traceback (most recent call last)
<ipython-input-21-0b1f36824e17> in <module>()
8 if(cropped_img == type(None)):
9 cropped_img = np.zeros(shape = (224, 224))
---> 10 img = (load_image(cropped_img) / 255.).astype(np.float32)
11 img = cv2.resize(img, dsize = (224,224))
12 embedding_vector = vgg_face_descriptor.predict(np.expand_dims(img, axis=0))[0]
<ipython-input-9-6d96fb74d85b> in load_image(path)
4 # OpenCV loads images with color channels
5 # in BGR order. So we need to reverse them
----> 6 return img[...,::-1]
TypeError: 'NoneType' object is not subscriptable
code is below
NoneType = type(None)
embedding =[]
for i, m in enumerate(metadata):
cropped_img = m.image_path()
print(i)
if(cropped_img == type(None)):
cropped_img = np.zeros(shape = (224, 224))
img = (load_image(cropped_img) / 255.).astype(np.float32)
img = cv2.resize(img, dsize = (224,224))
embedding_vector = vgg_face_descriptor.predict(np.expand_dims(img, axis=0))[0]
embedding.append(embedding_vector)
Load image code is as below:
def load_image(path):
img = cv2.imread(path, 1)
# OpenCV loads images with color channels
# in BGR order. So we need to reverse them
return img[...,::-1]
As I am new to python I can not understand what this error means
>>> None == type(None)
False
So, if cropped_img is None, your comparison if(cropped_img == type(None)): will be False. And thus, cropped_img = np.zeros(shape = (224, 224)) will never be executed, so cropped_img will remain None and will be passed on to load_image, which, as the error message shows, doesn't work with None.
You should check like this:
if cropped_img is None:
cropped_img = np.zeros(shape = (224, 224))
below code will resolve the problem as imread gives None
def load_image(path):
img = cv2.imread(path, 1)
if type(img) == type(None):
img = np.zeros(shape = (224, 224))
# OpenCV loads images with color channels
# in BGR order. So we need to reverse them
return img[...,::-1]

Categories