Search code examples
tensorflowtflearn

What do I change to adjust for the image going from grayscale to RGB?


I seem to consistently mess up and get lost on how to reshape the data to fit into a model. I think the input and output data shape has to match but I keep getting lost on how to go about this.

I think my main issue is that a gray scale image and a RGB image is stored differently. [1] vs [255,255,255]

So if:

screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)

was changed to:

screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)

everything works fine.

The code in question:

# Capture Data (CUT SHORT)
WIDTH = 160
HEIGHT = 120
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
screen = cv2.resize(screen, (WIDTH, HEIGHT))
dataset = []
output = [0, 0, 0, 0]
dataset.append([screen, output])
np.save("training.npy", dataset)

# Build Model
https://github.com/tflearn/tflearn/blob/master/examples/images/alexnet.py

# Changed to match output.
network = fully_connected(network, 4, activation='softmax')

# Train Data
WIDTH = 160
HEIGHT = 120
LR = 1e-3
EPOCHS = 5
MODEL_NAME = "HELP"

model = alexnet(WIDTH, HEIGHT, LR)

for i in range(EPOCHS):
    train_data = np.load("training.npy".format(i))

    train = train_data[:-100]
    test = train_data[-100:]

    X = np.array([i[0] for i in train]).reshape(-1,WIDTH,HEIGHT,1)
    Y = [i[1] for i in train]

    test_x = np.array([i[0] for i in test]).reshape(-1,WIDTH,HEIGHT,1)
    test_y = [i[1] for i in test]

    model.fit({'input': X}, {'targets': Y}, n_epoch=1, validation_set=({'input': test_x}, {'targets': test_y}), 
        snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

    model.save(MODEL_NAME)

ERROR: Exception in thread Thread-3: Traceback (most recent call last): File "C:\Users\TF\AppData\Local\Programs\Python\Python35\lib\threading.py", line 914, in _bootstrap_inner self.run() File "C:\Users\TF\AppData\Local\Programs\Python\Python35\lib\threading.py", line 862, in run self._target(*self._args, **self._kwargs) File "C:\Users\TF\AppData\Local\Programs\Python\Python35\lib\site-packages\tflearn\data_flow.py", line 187, in fill_feed_dict_queue data = self.retrieve_data(batch_ids) File "C:\Users\TF\AppData\Local\Programs\Python\Python35\lib\site-packages\tflearn\data_flow.py", line 222, in retrieve_data utils.slice_array(self.feed_dict[key], batch_ids) File "C:\Users\TF\AppData\Local\Programs\Python\Python35\lib\site-packages\tflearn\utils.py", line 187, in slice_array return X[start]

IndexError: index 2936 is out of bounds for axis 0 with size 1900


Solution

  • Dr. Robert Kirchgessner: You have three channels in your input dataset.

    np.array([i[0] for i in test]).reshape(-1,WIDTH,HEIGHT,3)
    

    in alexnet:

    network = input_data(shape=[None, width, height, 3], name='input')