The path is not written as I want. In my TRAIN_PATH, I have files 1, 2, 3, 4 inside each one I have two files, images (contain image tiff) and masks (contain mask png).
I don't know how to get access to each image or mask
I would be grateful for your help community
TRAIN_PATH = '/content/drive/MyDrive/PFE_MOHTICH/dataset/train'
TEST_PATH = '/content/drive/MyDrive/PFE_MOHTICH/dataset/test'
train_ids = next(os.walk(TRAIN_PATH))[1]
test_ids = next(os.walk(TEST_PATH))[1]
X_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)
Y_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)
print('Resizing training images and masks')
for n, i in tqdm(enumerate(train_ids), total=len(train_ids)):
path = TRAIN_PATH + i
img = imread(path + '/images/' + i + '.tiff')[:,:,:IMG_CHANNELS]
img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
X_train[n] = img #Fill empty X_train with values from img
mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)
for mask_file in next(os.walk(path + '/masks/'))[2]:
mask_ = imread(path + '/masks/' + mask_file)
mask_ = np.expand_dims(resize(mask_, (IMG_HEIGHT, IMG_WIDTH), mode='constant',
preserve_range=True), axis=-1)
mask = np.maximum(mask, mask_)
Y_train[n] = mask
***************************ERROR**********************
0%| | 0/4 [00:00<?, ?it/s]Resizing training images and masks
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-12-2ea116d05bcb> in <module>()
11 for n, i in tqdm(enumerate(train_ids), total=len(train_ids)):
12 path = TRAIN_PATH + i
---> 13 img = imread(path + '/images/' + i + '.tiff')[:,:,:IMG_CHANNELS]
14 img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
15 X_train[n] = img #Fill empty X_train with values from img
5 frames
/usr/local/lib/python3.7/dist-packages/tifffile/tifffile.py in open(self)
9457 self._file = os.path.realpath(self._file)
9458 self._dir, self._name = os.path.split(self._file)
-> 9459 self._fh = open(self._file, self._mode)
9460 self._close = True
9461 if self._offset is None:
FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/MyDrive/PFE_MOHTICH/dataset/train1/images/1.tiff'
If your image path is something like '.../dataset/train/1/images/1.tiff', try changing the path var to:
# for ...
path = TRAIN_PATH + '/' + i
#...
If instead the image path is something like '.../dataset/train/images/1.tiff', try changing the path var to:
# for ...
path = TRAIN_PATH
#...
Related
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?
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
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)
****i working in a project to segmenting liver from CT image volumes , the CT volumes have different number of slice so the shape of each volumes is different ex:(512,512,183) and (512,512,64) and (512,512,335) and so on**
i tried to use None and Global MaxPooling3D() as i seen in another post
but have same error which is :-
**
Traceback (most recent call last):
File "E:\Liver_Seg_Project\LiveSegtraining.py", line 123, in train
H = model.fit_generator(aug.flow(data, label, batch_size=100),
File "C:\python3.6.1\Python\lib\site-packages\keras_preprocessing\image\image_data_generator.py", line 430, in flow subset=subset
File "C:\python3.6.1\Python\lib\site-packages\keras_preprocessing\image\numpy_array_iterator.py", line 72, in init
(len(x), len(xx)))
ValueError: All of the arrays in x should have the same length. Found a pair with: len(x[0]) = 183, len(x[?]) = 64
Here is my model:
class ModelNw2:
#staticmethod
def build(depth,height, width):
input_size = (None,None,None,1)
x = Input(input_size)
# layer 1
x1=Conv3D(32, 7, padding="same",data_format="channels_last")(x)
x1=Activation("relu")(x1)
x1=MaxPooling3D(pool_size=(2, 2,2), strides=(2, 2,2))(x1)
# layer 2
x2=Conv3D(64, 5, padding="same")(x1)
x2=Activation("relu")(x2)
x2=MaxPooling3D(pool_size=(2, 2,2), strides=(2, 2,2))(x2)
# layer 3
x3=Conv3D(128, 5, padding="same")(x2)
x3=Activation("relu")(x3)
# layer 4
x4=Conv3D(128, 3, padding="same")(x3)
x4=Activation("relu")(x4)
# concat layer 3 and 4
concat34 = concatenate([x3,x4], axis = -1)
Here is part of training code
# initialize the data and labels
print("[INFO] loading images...")
data=[]
label=[]
for i in range(10):
if i>5:
j=i+10
filename ='TrainingF/image/liver-orig0' + str(j+1) + '.mhd'
else:
j=i
filename ='TrainingF/image/liver-orig00' + str(j+1) + '.mhd'
image = sitk.ReadImage(filename)
image = sitk.GetArrayFromImage(image)
image=Norma(image)
image = img_to_array(image)
data.append(image)
print("inssss=" + str(len(data)))
print("[INFO] loading maskss...")
for i in range(10):
if i>5:
j=i+10
# print("label "+ str(j+1))
filename ='TrainingF/label/liver-seg0' + str(j+1) + '.mhd'
else:
j=i
filename ='TrainingF/label/liver-seg00' + str(j+1) + '.mhd'
image = sitk.ReadImage(filename)
mask = sitk.GetArrayFromImage(image)
mask = img_to_array(mask)
label.append(mask)
Finally i Fit the data and mask numpy array to the model :
aug = ImageDataGenerator(rotation_range=45, width_shift_range=0.1,
height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
horizontal_flip=True, fill_mode="nearest")
# initialize the model
print("[INFO] compiling model...")
model = ModelNw2.build(depth=183,width=512, height=512)
# train the network
print("[INFO] training network...")
weight_saver = ModelCheckpoint('weights1.h1', monitor='val_dice_coef', save_best_only=True, save_weights_only=True)
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.8 ** x)
stop_here = EarlyStopping(patience=5)
start = timeit.default_timer()
H = model.fit_generator(aug.flow(data, label, batch_size=100),
validation_data=(testX, testY), steps_per_epoch=50,
epochs=EPOCHS, verbose=2, callbacks = [weight_saver, annealer])
end = timeit.default_timer()
I'm learning about image classification in keras. I've downloaded sample dataset of donuts and waffles, but they differ in size. To standardise their size I'm loading images from their directories, resize them and store them in numpy arrays:
test_data_dir = 'v_data/train/donuts_and_waffles/'
validation_data_dir = 'v_data/test/donuts_and_waffles/'
loaded_test_donuts = list()
for filename in listdir(test_data_dir + 'donuts/'):
image1 = Image.open(test_data_dir + 'donuts/' + filename)
img_resized = image1.resize((224,224))
img_data = asarray(img_resized)
loaded_test_donuts.append(img_data)
loaded_test_waffles = list()
for filename in listdir(test_data_dir + 'waffles/'):
image1 = Image.open(test_data_dir + 'waffles/' + filename)
img_resized = image1.resize((224,224))
img_data = asarray(img_resized)
loaded_test_waffles.append(img_data)
loaded_validation_donuts = list()
for filename in listdir(validation_data_dir + 'donuts/'):
image1 = Image.open(validation_data_dir + 'donuts/' + filename)
img_resized = image1.resize((224,224))
img_data = asarray(img_resized)
loaded_validation_donuts.append(img_data)
loaded_validation_waffles = list()
for filename in listdir(validation_data_dir + 'waffles/'):
image1 = Image.open(validation_data_dir + 'waffles/' + filename)
img_resized = image1.resize((224,224))
img_data = asarray(img_resized)
loaded_validation_waffles.append(img_data)
test_data = list()
validation_data = list()
test_data.append(np.array(loaded_test_donuts))
test_data.append(np.array(loaded_test_waffles))
validation_data.append(np.array(loaded_validation_donuts))
validation_data.append(np.array(loaded_validation_waffles))
test_data = np.array(test_data)
validation_data = np.array(validation_data)
Then I want to create an ImageDataGenerator for my data:
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow(
#how can I pass here test_data to make it work (along with which parameters)
)
validation_generator = test_datagen.flow(
#how can I pass here validation_data to make it work (along with which parameters)
)
How to achieve that?
I have tried like this:
train_generator = train_datagen.flow(
test_data, #does not work
batch_size=batch_size)
validation_generator = test_datagen.flow(
validation_data, #does not work
batch_size=batch_size)
but then I get this error:
Traceback (most recent call last):
...
ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (2, 770, 224, 224, 3))
It's hard to say what does not work without error message, but I assume the problem is that you pass lists to your ImageDataGenerators. You can fix this easily by converting your lists to numpy-arrays:
test_data = list()
validation_data = list()
test_data.append(np.array(loaded_test_donuts))
test_data.append(np.array(loaded_test_waffles))
validation_data.append(np.array(loaded_validation_donuts))
validation_data.append(np.array(loaded_validation_waffles))
test_data = np.array(test_data)
validation_data = np.array(validation_data)
Edit: A better way, stacking instead of appending to lists and converting
test_data = np.vstack((np.array(loaded_test_donuts),np.array(loaded_test_waffles)))
validation_data = np.vstack((np.array(loaded_validation_donuts),np.array(loaded_validation_waffles)))
What I would recommend is that you create a folder where you have n folders representing your classes such as "dog", "cat" and do the preprocessing step first and then save the produced images like this:
from PIL import Image
import glob
from keras.preprocessing import image
W=500
H=825
for folder in glob.glob("*"): #goes through every folder
ims = glob.glob(folder+ "\\*.png") #reads image names from folder assuming images are png
for im in ims:
img = Image.open(im)
print(im)
if (img.size != (W, H)):
imgr = process(img, W, H) # where "process" is reszing in your case
imgr.save(im)
then spilt your data into train and validation folders and do:
traingen = image.ImageDataGenerator(rescale=1./255)
validationgen = image.ImageDataGenerator(rescale=1./255)
train = traingen.flow_from_directory("train",target_size=(H,W), batch_size=s,shuffle=True)
val = validationgen.flow_from_directory("validation",target_size=(500, 825), batch_size=32, shuffle=False)
You test_data does not have the correct shape, you have to convert into an array of shape 4 for example (770, 224, 224, 3), 770 refers the number of the images, 224x224 refers to the size of the images (pixels) and the 3 refers the color of the images.