wrong input of image in tensorflow for training - python

I am writing my thesis in machine learning and am trying to build a unet to perform it. The code is as follows:
First i create the dataloader to create the datasets for input:
def dataloader(filepath, subset):
# Initiliaze return arrays - input of shape = HYPERPARAMETER
global size
if subset=="train":
size = 129
elif subset=="test":
size = 18
input_data=np.zeros((size,1024,1024,1))
output_data=np.zeros((size,1024,1024,1))
# Open file and create loop
with open(filepath+"annotation_"+subset+".txt", "r") as input_file:
# Count to pass through the file
count=0
for line in input_file:
line=line.split(" ")
data=cv2.imread(filepath+str(line[0])+".jpg",cv2.IMREAD_GRAYSCALE)
input_data[count,:,:,0]=data
# Case of benevolent
if line[3]=="B":
x=int(line[4])
y=1024-int(line[5])
radius=int(line[6])
for i in range(1024):
for j in range(1024):
if ((radius*radius-(i-x)*(i-x)-(j-y)*(j-y))>0):
# Setting 80 as th value of the benevolent mask
output_data[count,i,j,0]=80
# Case of malevolent
elif line[3]=="M":
x=int(line[4])
y=1024-int(line[5])
radius=int(line[6])
for i in range(1024):
for j in range(1024):
if ((radius*radius-(i-x)*(i-x)-(j-y)*(j-y))>0):
# Setting 160 as th value of the benevolent mask
output_data[count,i,j,0]=160
if count==0:
print(type(data))
print(type(input_data))
cv2.imshow('test',data)
cv2.waitKey(0)
cv2.imshow('image',input_data[count,:,:,0])
cv2.waitKey(0)
cv2.imshow('mask',output_data[count,:,:,0])
cv2.waitKey(0)
cv2.destroyAllWindows()
count=count+1
#input_data=K.zeros_like(input_data)
#output_data=K.zeros_like(output_data)
return input_data, output_data
and then the model and the commands to run it:
def unet_model(optimizer, loss_metric, metrics, sample_width, sample_height, lr=1e-3):
inputs = Input((sample_width, sample_height, 1))
print(inputs.shape)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
drop1 = Dropout(0.5)(pool1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(drop1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
drop2 = Dropout(0.5)(pool2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(drop2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
drop3 = Dropout(0.3)(pool3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(drop3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
drop4 = Dropout(0.3)(pool4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(drop4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)
up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv5), conv4], axis=3)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)
up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv6), conv3], axis=3)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)
up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv7), conv2], axis=3)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)
up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv8), conv1], axis=3)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)
conv10 = Conv2D(1, (1, 1), activation='softmax')(conv9)
model = Model(inputs=[inputs], outputs=[conv10])
model.compile(optimizer=optimizer(lr=lr), loss=loss_metric, metrics=metrics)
return model
#Filepath of datasets
filepath = "/home/tzikos/Downloads/train/"
# Load datasets
train_input, train_output = dataloader(filepath, "train")
test_input, test_output = dataloader(filepath, "test")
train_input = normalize(train_input)
test_input = normalize(test_input)
train_output = normalize(train_output)
test_output = normalize(test_output)
print(train_input.shape)
print(train_output.shape)
print(test_input.shape)
print(test_output.shape)
# Load model
model = unet_model(optimizer=Adam, loss_metric=tf.keras.losses.MeanSquaredError(), metrics=["accuracy"], sample_width=train_input.shape[1], sample_height=train_input.shape[2],lr=1e-3)
model.compile(optimizer="Adam", loss=tf.keras.losses.MeanSquaredError(), metrics=["accuracy"])
history = model.fit(x=train_input, y=train_output, batch_size=1, epochs=30)
# Save weights
model_filepath = '/home/tzikos/Desktop/thesis_DENSE-IN-UNET/unet_weights.h5'
model.save(model_filepath)
# Check results
results = model.evaluate(test_input, test_output)
print(results)
So the problem is the following:
When I train my model i get 0 accuracy and no change in the loss function. So I went and dived into the images.
When I imshow the data variable i get the photo as should be. However when I input it into the numpy array it is transcribed into a binary one where there is black and white and idk why.
So I think that is the problem but i cant see why that is since the data variable is allright

I am not sure what this network is supposed to do, so I will list some mistakes that in your code.
In your function you already compile the model:
model.compile(optimizer=optimizer(lr=lr), loss=loss_metric, metrics=metrics)
return model
After outside the function you do it again:
model.compile(optimizer="Adam", loss=tf.keras.losses.MeanSquaredError(), metrics=["accuracy"])
One issue is about metric and loss. tf.keras.losses.MeanSquaredError() is a regression loss and 'accuracy' is a classification metric.
The most obvious one:
conv10 = Conv2D(1, (1, 1), activation='softmax')(conv9)
Softmax activation will be applied to your last axis. If you check your model.summary() your last axis consist of size 1 which means you have a single element. So you are just returning(outputting) a vector of ones everytime.

Related

TypeError: Inputs to a layer should be tensors. unet model

X_train = np.array(X_train)
X_test = np.array(X_test)
y_train = np.array(y_train)
y_test = np.array(y_test)
def unet():
# First block Going down
d1_2 = Conv2D(16, (3, 3), activation='relu', padding='same',input_shape=(s,s,3))
d1_3 = Conv2D(16, (3, 3), activation='relu', padding='same')(d1_2)
# Second block Going down
d2_1 = MaxPooling2D()(d1_3)
d2_2 = Conv2D(32, (3, 3), activation='relu', padding='same')(d2_1)
d2_3 = Conv2D(32, (3, 3), activation='relu', padding='same')(d2_2)
# Third block Going down
d3_1 = MaxPooling2D()(d2_3)
d3_2 = Conv2D(64, (3, 3), activation='relu', padding='same')(d3_1)
d3_3 = Conv2D(64, (3, 3), activation='relu', padding='same')(d3_2)
# Fourth block Going down
d4_1 = MaxPooling2D()(d3_3)
d4_2 = Conv2D(128, (3, 3), activation='relu', padding='same')(d4_1)
d4_3 = Conv2D(128, (3, 3), activation='relu', padding='same')(d4_2)
# Fifth block
d5_1 = MaxPooling2D()(d4_3)
d5_2 = Conv2D(256, (3, 3), activation='relu', padding='same')(d5_1)
d5_3 = Conv2D(256, (3, 3), activation='relu', padding='same')(d5_2)
# Fourth block going up, concatenated with Fourth block going down
up4_0 = UpSampling2D((2, 2))(d5_3)
up4_1 = tf.keras.layers.concatenate([d4_3, up4_0])
up4_2 = Conv2D(128, (3, 3), activation='relu', padding='same')(up4_1)
up4_3 = Conv2D(128, (3, 3), activation='relu', padding='same')(up4_2)
# Third block going up, concatenated with Third block going down
up3_0 = UpSampling2D((2, 2))(up4_3)
up3_1 = tf.keras.layers.concatenate([d3_3, up3_0])
up3_2 = Conv2D(64, (3, 3), activation='relu', padding='same')(up3_1)
up3_3 = Conv2D(64, (3, 3), activation='relu', padding='same')(up3_2)
# Second block going up, concatenated with Second block going down
up2_0 = UpSampling2D((2, 2))(up3_3)
up2_1 = tf.keras.layers.concatenate([d2_3, up2_0])
up2_2 = Conv2D(32, (3, 3), activation='relu', padding='same')(up2_1)
up2_3 = Conv2D(32, (3, 3), activation='relu', padding='same')(up2_2)
# First block going up, concatenated with First block going down
up1_0 = UpSampling2D((2, 2))(up2_3)
up1_1 = tf.keras.layers.concatenate([d1_3, up1_0])
up1_2 = Conv2D(16, (3, 3), activation='relu', padding='same')(up1_1)
up1_3 = Conv2D(16, (3, 3), activation='relu', padding='same')(up1_2)
# Output
out = Conv2D(1, (1, 1), activation='sigmoid', padding='same')(up1_3)
return out
some answer to a similar question suggested using as_numpy_iterator.next() so I'm passing one batch at a time instead of the whole dataset
from keras.layers import Input,Conv2D,MaxPooling2D,UpSampling2D, BatchNormalization
from keras.models import Model
unet = Model(unet(),input_shape = (s,s,3))
unet.compile(loss='mean_squared_error', optimizer = optimizers.rmsprop_v2.RMSprop())
unet_train = unet.fit(X_train.as_numpy_iterator.next(), y_train.as_numpy_iterator.next(), batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(X_test, y_test))
still run into the same error
TypeError: Inputs to a layer should be tensors. Got: <keras.layers.convolutional.conv2d.Conv2D object
I think the issue at the beginning of 'unet' function:
# First block Going down
d1_2 = Conv2D(16, (3, 3), activation='relu', padding='same',input_shape=(s,s,3))
Please try instead:
# First block Going down
inputs = Input(shape=(s,s,3))
d1_2 = Conv2D(16, (3, 3), activation='relu', padding='same')(inputs)
For more details please check:
https://keras.io/guides/functional_api/
"...To build this model using the functional API, start by creating an input node."

Keras: Using Dice coefficient Loss Function, val loss is not improving

Problem
I am doing two classes image segmentation, and I want to use loss function of dice coefficient. However validation loss is not improved. How to Solve these problem?
what I did
Using the mothod of one-hot encoding, Processed label image and it has not include backgroung label.
Code
Shape of X is (num of data, 256, 256, 1) # graysacle
Shape of y is (num of data, 256, 256, 2) # two class and exclude background label
one_hot_y = np.zeros((len(y), image_height, image_width, 2))
for i in range(len(y)):
one_hot = to_categorical(y[i])
one_hot_y[i] = one_hot[:,:,1:]
one_hot_y.shape #-> (566, 256, 256, 2)
#### <-- Unet Model --> ####
from tensorflow import keras
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Concatenate, Conv2DTranspose
from keras import Model
def unet(image_height, image_width, num_classes):
# inputs = Input(input_size)
inputs = Input(shape=(image_height, image_width, 1),name='U-net')
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)
up6 = Concatenate()([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv5), conv4])
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)
up7 = Concatenate()([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv6), conv3])
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)
up8 = Concatenate()([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv7), conv2])
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)
up9 = Concatenate()([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv8), conv1])
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)
outputs = Conv2D(num_classes, (1, 1), activation='softmax')(conv9)
return Model(inputs=[inputs], outputs=[outputs])```
#### <-- Dice Score --> ####
from tensorflow.keras import backend as K
def dice_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection + 0.0001) / (K.sum(y_true_f) + K.sum(y_pred_f) + 0.0001)
def dice_coef_loss(y_true, y_pred):
return 1 - dice_coef(y_true, y_pred)```
#### <-- Fit the Model --> ####
from tensorflow.keras import optimizers
adam = optimizers.Adam(learning_rate=0.0001)
unet_model.compile(optimizer=adam, loss=[dice_coef_loss],metrics=[dice_coef])
hist = unet_model.fit(X_train,y_train, epochs=epochs, batch_size=batch_size,validation_data=(X_val,y_val), callbacks=[checkpoint,earlystopping])
I tried to replicate your experience. I used the Oxford-IIIT Pets database whose label has three classes: 1: Foreground, 2: Background, 3: Not classified. If class 1 ("Foreground") is removed as you did, then the val_loss does not change during the iterations. On the other hand, if the "Not classified" class is removed, the optimization seems to work. The model fails to discriminate between "Background" and "Not classified", which is conceivable.
Besides, there is a small error in the calculation of the dice coefficient: In the denominator, you need to take the sum of the squares. It doesn't change anything for y_true but for y_pred it does.
I can't say why your code doesn't work, but I can tell you the way I do it. Differences are that I exclude the background and encode the target inside the dice coef calculation function.
Then I define my Dice coefficient as follows:
def dice_coef(y_true, y_pred, smooth=1):
# flatten
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
# one-hot encoding y with 3 labels : 0=background, 1=label1, 2=label2
y_true_f = K.one_hot(K.cast(y_true_f, np.uint8), 3)
y_pred_f = K.one_hot(K.cast(y_pred_f, np.uint8), 3)
# calculate intersection and union exluding background using y[:,1:]
intersection = K.sum(y_true_f[:,1:]* y_pred_f[:,1:], axis=[-1])
union = K.sum(y_true_f[:,1:], axis=[-1]) + K.sum(y_pred_f[:,1:], axis=[-1])
# apply dice formula
dice = K.mean((2. * intersection + smooth)/(union + smooth), axis=0)
return dice
def dice_loss(y_true, y_pred):
return 1-dice_coef
I was also confused about this problem until I understood the following code!!!!
import numpy as np
from PIL import Image
from keras import backend as K
def dice_loss(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f* y_pred_f)
val = (2. * intersection + K.epsilon()) / (K.sum(y_true_f * y_true_f) + K.sum(y_pred_f * y_pred_f) + K.epsilon())
return 1. - val
arr1 = np.array([[[9.6,0.6,0.3],
[0.3,0.5,0.5]],
[[0.5,0.5,0.5],
[0.5,0.5,0.5]],
[[0.5,0.5,0.5],
[0.5,0.5,0.5]],
[[0.5,0.5,0.5],
[0.5,0.5,0.5]]])
arr2= np.array([[[9.6,0.6,0.3],
[0.3,0.5,0.5]],
[[0.5,0.5,0.5],
[0.5,0.5,0.5]],
[[0.5,0.5,0.5],
[0.5,0.5,0.5]],
[[0.5,0.5,0.5],
[0.5,0.5,0.5]]])
loss = dice_loss(arr1,arr2)
print(loss)

Why does my Keras/TensorFlow model refuse to fit (even though params appear correct)?

Using this Model:
def unet(input_shape=(256, 256, 256)):
inputs = Input(input_shape)
conv1 = Conv2D(64, (3, 3), padding='same')(inputs)
#print(conv1.shape)
conv1 = BatchNormalization()(conv1)
conv1 = Activation('relu')(conv1)
conv1 = Conv2D(64, (3, 3), padding='same')(conv1)
conv1 = BatchNormalization()(conv1)
conv1 = Activation('relu')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(conv1)
conv2 = Conv2D(128, (3, 3), padding='same')(pool1)
conv2 = BatchNormalization()(conv2)
conv2 = Activation('relu')(conv2)
conv2 = Conv2D(128, (3, 3), padding='same')(conv2)
conv2 = BatchNormalization()(conv2)
conv2 = Activation('relu')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(conv2)
conv3 = Conv2D(256, (3, 3), padding='same')(pool2)
conv3 = BatchNormalization()(conv3)
conv3 = Activation('relu')(conv3)
conv3 = Conv2D(256, (3, 3), padding='same')(conv3)
conv3 = BatchNormalization()(conv3)
conv3 = Activation('relu')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(conv3)
conv4 = Conv2D(512, (3, 3), padding='same')(pool3)
conv4 = BatchNormalization()(conv4)
conv4 = Activation('relu')(conv4)
conv4 = Conv2D(512, (3, 3), padding='same')(conv4)
conv4 = BatchNormalization()(conv4)
conv4 = Activation('relu')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(conv4)
max = Conv2D(1024, (3, 3), padding='same')(pool4)
max = BatchNormalization()(max)
max = Activation('relu')(max)
max = Conv2D(1024, (3, 3), padding='same')(max)
max = BatchNormalization()(max)
max = Activation('relu')(max)
back4 = UpSampling2D((2, 2))(max)
back4 = concatenate([conv4, back4], axis = 3)
back4 = Conv2D(512, (3, 3), padding='same')(back4)
back4 = BatchNormalization()(back4)
back4 = Activation('relu')(back4)
back4 = Conv2D(512, (3, 3), padding='same')(back4)
back4 = BatchNormalization()(back4)
back4 = Activation('relu')(back4)
back4 = Conv2D(512, (3, 3), padding='same')(back4)
back4 = BatchNormalization()(back4)
back4 = Activation('relu')(back4)
back3 = UpSampling2D((2, 2))(back4)
back3 = concatenate([conv3, back3], axis = 3)
back3 = Conv2D(256, (3, 3), padding='same')(back3)
back3 = BatchNormalization()(back3)
back3 = Activation('relu')(back3)
back3 = Conv2D(256, (3, 3), padding='same')(back3)
back3 = BatchNormalization()(back3)
back3 = Activation('relu')(back3)
back3 = Conv2D(256, (3, 3), padding='same')(back3)
back3 = BatchNormalization()(back3)
back3 = Activation('relu')(back3)
back2 = UpSampling2D((2, 2))(back3)
back2 = concatenate([conv2, back2], axis = 3)
back2 = Conv2D(128, (3, 3), padding='same')(back2)
back2 = BatchNormalization()(back2)
back2 = Activation('relu')(back2)
back2 = Conv2D(128, (3, 3), padding='same')(back2)
back2 = BatchNormalization()(back2)
back2 = Activation('relu')(back2)
back2 = Conv2D(128, (3, 3), padding='same')(back2)
back2 = BatchNormalization()(back2)
back2 = Activation('relu')(back2)
back1 = UpSampling2D((2, 2))(back2)
back1 = concatenate([conv1, back1], axis = 3)
back1 = Conv2D(64, (3, 3), padding='same')(back1)
back1 = BatchNormalization()(back1)
back1 = Activation('relu')(back1)
back1 = Conv2D(64, (3, 3), padding='same')(back1)
back1 = BatchNormalization()(back1)
back1 = Activation('relu')(back1)
back1 = Conv2D(64, (3, 3), padding='same')(back1)
back1 = BatchNormalization()(back1)
back1 = Activation('relu')(back1)
outputs = Conv2D(1, (1, 1), activation='sigmoid')(back1)
#np_from_tensor = tf.make_ndarray(outputs)
#print(type(outputs))
#output_tf_array.append(outputs)
model = Model(inputs = [inputs], outputs = [outputs])
model.summary()
model.compile(optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999), loss='binary_crossentropy', metrics=['accuracy'])
return model
My model has the following parameters passed to the Model.fit() method:
X_train (a 'List' object containing 4 numpy.ndarray objects with Shape(256, 256, 256))
y_train (a numpy.ndarray object with shape (4, 5))
When passed to Model.fit(X_train, y_train, steps_per_epoch=10, epochs=100), I get the following error:
ValueError: Data cardinality is ambiguous:
x sizes: 256, 256, 256, 256
y sizes: 4
Please provide data which shares the same first dimension.
I think the params to the Model.fit() method are correct because x sizes refers to all elements from the List and y sizes is how many elements there are in the List.
I have tried converting X_train to a numpy.ndarray type, but Model.fit() does not accept that type as the first param, and I don't think reshaping y_train to have Shape(20) is correct because y sizes still refers to how many numpy.ndarray objects there are in X_train 'List' object.
Are there any other means which could yield my Model.fit(X_train, y_train, steps_per_epoch=10, epochs=100) to not raise the ValueError?
Update:
After resolving the above error, I get another error saying :
ValueError: logits and labels must have the same shape ((1, 256, 256, 1) vs (1, 5))
Somehow, I don't believe reshaping could help per se, so how might I otherwise 'match' the labels?
Thanks.
Your model has only one input, so convert your list to np.ndarray:
input = np.asarray(input)
And your model expecting shape (4, 256, 256, 1) as labels. It doesn't tie to your label shape (4, 5).

custom loss function with multiple output and using add_loss

I have an autoencoder in keras and I need a loss function which is the combination of mse, a binary_crossentropy and third part that tries to make minimum the number of pixels of output that have a value different from 0 or 1. the final loss should be like this: amse+bbinary_crossentropy+c*L. I used the below code for this but it produces this error:
Traceback (most recent call last): File
"", line 134, in if
(pred_w(i,j)>=0 & pred_w(i,j)<0.1)|(pred_w(i,j)<=1 &
pred_w(i,j)>=0.9): TypeError: 'Tensor' object is not callable
could you please tell me what should I do to solve this problem? I appreciate your help.
wtm=Input((4,4,1))
image = Input((28, 28, 1))
conv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1e')(image)
conv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2e')(conv1)
conv3 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl3e')(conv2)
#conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='convl3e', kernel_initializer='Orthogonal',bias_initializer='glorot_uniform')(conv2)
BN=BatchNormalization()(conv3)
encoded = Conv2D(1, (5, 5), activation='relu', padding='same',name='encoded_I')(BN)
#-----------------------adding w---------------------------------------
wpad=Kr.layers.Lambda(lambda xy: xy[0] + Kr.backend.spatial_2d_padding(xy[1], padding=((0, 24), (0, 24))))
encoded_merged=wpad([encoded,wtm])
#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
deconv1 = Conv2D(64, (5, 5), activation='elu', padding='same', name='convl1d')(encoded_merged)
deconv2 = Conv2D(64, (5, 5), activation='elu', padding='same', name='convl2d')(deconv1)
deconv3 = Conv2D(64, (5, 5), activation='elu',padding='same', name='convl3d')(deconv2)
deconv4 = Conv2D(64, (5, 5), activation='elu',padding='same', name='convl4d')(deconv3)
BNd=BatchNormalization()(deconv4)
decoded = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='decoder_output')(BNd)
model=Model(inputs=[image,wtm],outputs=decoded)
decoded_noise = GaussianNoise(0.5)(decoded)
#----------------------w extraction------------------------------------
convw1 = Conv2D(64, (5,5), activation='relu', name='conl1w')(decoded_noise)#24
convw2 = Conv2D(64, (5,5), activation='relu', name='convl2w')(convw1)#20
convw3 = Conv2D(64, (5,5), activation='relu' ,name='conl3w')(convw2)#16
convw4 = Conv2D(64, (5,5), activation='relu' ,name='conl4w')(convw3)#12
convw5 = Conv2D(64, (5,5), activation='relu', name='conl5w')(convw4)#8
convw6 = Conv2D(64, (5,5), activation='relu', name='conl6w')(convw5)#4
convw7 = Conv2D(64, (5,5), activation='relu',padding='same', name='conl7w',dilation_rate=(2,2))(convw6)#4
convw8 = Conv2D(64, (5,5), activation='relu', padding='same',name='conl8w',dilation_rate=(2,2))(convw7)#4
convw9 = Conv2D(64, (5,5), activation='relu',padding='same', name='conl9w',dilation_rate=(2,2))(convw8)#4
convw10 = Conv2D(64, (5,5), activation='relu',padding='same', name='conl10w',dilation_rate=(2,2))(convw9)#4
BNed=BatchNormalization()(convw10)
pred_w = Conv2D(1, (1, 1), activation='sigmoid', padding='same', name='reconstructed_W',dilation_rate=(2,2))(BNed)
w_extraction=Model(inputs=[image,wtm],outputs=[decoded,pred_w])
count=0
for i in range(28):
for j in range(28):
if (pred_w(i,j)>=0 & pred_w(i,j)<0.1)|(pred_w(i,j)<=1 & pred_w(i,j)>=0.9):
count+=1
loss = K.sum(0.7*mse(decoded, image),binary_crossentropy(pred_w,wtm))+count
w_extraction.add_loss(loss)
Technically the error just tells you that pred_w(i,j) should be pred_w[i,j] if you want to refer to an entry of the matrix. However, to make this code as you intend it to run requires quite a bit more rewrite.
To actually optimize a loss the main thing would be that it needs to be differentiable with regard to the weights in the networks. Examples you could use in this case could be:
(x-0.5)^N for a relatively high N
or log-barriers, i.e. -log(x)-log(1-x)
actually counting the numbers (which will not help for optimization) could be achieved with something like
count = tf.sum(pred_w<=0.1) + tf.sum(predictions_w>=0.9)
Maybe this is helpful to output how many numbers are in that range during training or something like this.

Python restarts without implementing my code

I'm trying to run my code using the python idle. But when ever I'mtrying to run this , after a specific line the shell is getting restarted
from __future__ import print_function
import numpy as np
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D
from keras.layers import concatenate
from keras.optimizers import Adam
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras import backend as K
K.set_image_dim_ordering('th') # Theano dimension ordering in this code
img_rows = 512
img_cols = 512
smooth = 1.
def dice_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def dice_coef_np(y_true,y_pred):
y_true_f = y_true.flatten()
y_pred_f = y_pred.flatten()
intersection = np.sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (np.sum(y_true_f) + np.sum(y_pred_f) + smooth)
def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)
def get_unet():
inputs = Input((1,img_rows, img_cols))
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)
#up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1)
up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], axis=1)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)
#up7 = merge([UpSampling2D(size=(2, 2))(conv6), conv3], mode='concat', concat_axis=1)
up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=1)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)
#up8 = merge([UpSampling2D(size=(2, 2))(conv7), conv2], mode='concat', concat_axis=1)
up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=1)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)
#up9 = merge([UpSampling2D(size=(2, 2))(conv8), conv1], mode='concat', concat_axis=1)
up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=1)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)
conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv9)
model = Model(inputs=inputs, outputs=conv10)
model.compile(optimizer=Adam(lr=1.0e-5), loss=dice_coef_loss, metrics=[dice_coef])
return model
def train_and_predict(use_existing):
print('-'*30)
print('Loading and preprocessing train data...')
print('-'*30)
imgs_train = np.load("C:/Users/hirplk/Desktop/unet/Luna2016-Lung-Nodule-Detection-master_new/DATA_PROCESS/scratch/cse/dual/cs5130287/Luna2016/output_final/"+"trainImages.npy").astype(np.float32)
imgs_mask_train = np.load("C:/Users/hirplk/Desktop/unet/Luna2016-Lung-Nodule-Detection-master_new/DATA_PROCESS/scratch/cse/dual/cs5130287/Luna2016/output_final/"+"trainMasks.npy").astype(np.float32)
imgs_test = np.load("C:/Users/hirplk/Desktop/unet/Luna2016-Lung-Nodule-Detection-master_new/DATA_PROCESS/scratch/cse/dual/cs5130287/Luna2016/output_final/"+"testImages.npy").astype(np.float32)
imgs_mask_test_true = np.load("C:/Users/hirplk/Desktop/unet/Luna2016-Lung-Nodule-Detection-master_new/DATA_PROCESS/scratch/cse/dual/cs5130287/Luna2016/output_final/"+"testMasks.npy").astype(np.float32)
mean = np.mean(imgs_train) # mean for data centering
std = np.std(imgs_train) # std for data normalization
imgs_train -= mean # images should already be standardized, but just in case
imgs_train /= std
print('-'*30)
print('Creating and compiling model...')
print('-'*30)
model = get_unet()
# Saving weights to unet.hdf5 at checkpoints
model_checkpoint = ModelCheckpoint('unet.hdf5', monitor='loss', save_best_only=True)
#
# Should we load existing weights?
# Set argument for call to train_and_predict to true at end of script
if use_existing:
model.load_weights('./unet.hdf5')
#
# The final results for this tutorial were produced using a multi-GPU
# machine using TitanX's.
# For a home GPU computation benchmark, on my home set up with a GTX970
# I was able to run 20 epochs with a training set size of 320 and
# batch size of 2 in about an hour. I started getting reseasonable masks
# after about 3 hours of training.
#
print('-'*30)
print('Fitting model...')
print('-'*30)
model.fit(imgs_train, imgs_mask_train, batch_size=2, epochs=10, verbose=1, shuffle=True,
callbacks=[model_checkpoint])
print ('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')
This is the output of idle.
RESTART: C:\Users\hirplk\Desktop\unet\DSB3Tutorial-master\tutorial_code\LUNA_train_unet.py
Warning (from warnings module):
File "C:\Research\Python_installation\lib\site-packages\h5py\__init__.py", line 36
from ._conv import register_converters as _register_converters
FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
Using TensorFlow backend.
------------------------------
Loading and preprocessing train data...
------------------------------
------------------------------
Creating and compiling model...
------------------------------
------------------------------
Fitting model...
------------------------------
Epoch 1/10
=============================== RESTART: Shell ===============================
Does this meant my python has crashed? Has anyone experienced this? It worked fine earlier but now I do not have any way of implementing my code
Do I need to reinstall everything from the beginning

Categories

Resources