How to put skimage imread_collection through tensorflow

Asked By: Anonymous

I’m trying to put a collection of images through a neural network, but I can’t figure out how to get a large collection of images to go into a tensorflow model, as trying to convert the collection into a numpy array causes a memory error.

I should note that I am very new to tensorflow.

import numpy as np
from skimage.io import imread_collection
from tensorflow import keras
from tensorflow.keras import layers

def gen(arr):return(i.reshape(400*600*3) for i in arr) # Only used in Attempt2.

labelFile=open("lables_text_file.txt","r")
labels=labelFile.read()
labelFile.close()
labels=getTrain(labels)#Converts to a tuple containing the lables in order.

data = imread_collection("path_to_images/*.jpg", conserve_memory=True)
train=data[:-len(data)//4]
trainLabels=labels[:-len(data)//4]
test=data[-len(data)//4:]
testLabels=labels[-len(data)//4:]

#train = train.reshape(-1, 400*600*3) # Attempt1
#test = test.reshape(-1, 400*600*3) # Attempt1
#train = gen(train) # Attempt2
#test = gen(test) # Attempt2
trainLabels = keras.utils.to_categorical(trainLabels, 23)
testLabels = keras.utils.to_categorical(testLabels, 23)

model=keras.Sequential([keras.Input(shape=(400*600*3,)),
        layers.Dense(600, name='hidden1', activation='relu'),
        layers.Dense(400, name='hidden2', activation='relu'),
        layers.Dense(46, name='hidden3', activation='relu'),
        layers.Dense(23, activation="softmax")])

optimizer = keras.optimizers.Adam(learning_rate=0.0015)
model.compile(loss=keras.losses.CategoricalCrossentropy(), optimizer=optimizer, metrics=[keras.metrics.CategoricalAccuracy()])
model.fit(train,trainLabels,batch_size=128,epochs=8,validation_data=(test,testLabels), shuffle=True)

When I run the code as is, this is the result:

ValueError: Failed to find data adapter that can handle input: <class 'skimage.io.collection.ImageCollection'>, <class 'numpy.ndarray'>

When I try to use Attempt1, this is the result:

AttributeError: 'ImageCollection' object has no attribute 'reshape'

When I try to use Attempt2, this is the result:

ValueError: `y` argument is not supported when using python generator as input.

How can I put the data into `model.fit, such that it will successfully train the neural network?


Solution

Answered By: Anonymous

I think I may have solved the problems.

Working code:

import numpy as np
from skimage.io import imread_collection
from tensorflow import keras
from tensorflow.keras import layers

def gen(arr,labels):return((arr[i].reshape(-1,400*600*3),labels[i].reshape(-1,23)) for i in range(len(arr)))

labelFile=open("lables_text_file.txt","r")
labels=labelFile.read()
labelFile.close()
labels=getTrain(labels)#Converts to a tuple containing the lables in order.

data = imread_collection("path_to_images/*.jpg", conserve_memory=True)
train=data[:-len(data)//4]
trainLabels=labels[:-len(data)//4]
test=data[-len(data)//4:]
testLabels=labels[-len(data)//4:]

#train = train.reshape(-1, 400*600*3) # Attempt1
#test = test.reshape(-1, 400*600*3) # Attempt1
trainLabels = keras.utils.to_categorical(trainLabels, 23)
testLabels = keras.utils.to_categorical(testLabels, 23)
train = gen(train,trainLabels) # Attempt2
test = gen(test,testLabels) # Attempt2

model=keras.Sequential([keras.Input(shape=(400*600*3,)),
        layers.Dense(600, name='hidden1', activation='relu'),
        layers.Dense(400, name='hidden2', activation='relu'),
        layers.Dense(46, name='hidden3', activation='relu'),
        layers.Dense(23, activation="softmax")])

optimizer = keras.optimizers.Adam(learning_rate=0.0015)
model.compile(loss=keras.losses.CategoricalCrossentropy(), optimizer=optimizer, metrics=[keras.metrics.CategoricalAccuracy()])
model.fit(train,None,batch_size=128,epochs=8,validation_data=(test,testLabels), shuffle=True)

The solution was to pass in a generator that returns two-tuples containing the input and label (instead of passing the labels in directly), but there were other problems that I may include in this answer if I get the time.

techinplanet staff

techinplanet staff


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno