问题
I am new in using tensorflow. I am trying to train my network with images of shape (16*16). I have divided 3 grayscale images of 512*512 into 16*16 and appended all. so i have 3072*16*16. while training I am getting error. I am using jupyter notebook.Can anyone please help me?
Here is the code
import tensorflow as tf
import numpy as np
from numpy import newaxis
import glob
import os
from PIL import Image,ImageOps
import random
from os.path import join
import matplotlib.pyplot as plt
from tensorflow import keras
TRAIN_PATH = 'dataset/2/*.jpg'
LOGS_Path = "dataset/logs/"
CHECKPOINTS_PATH = 'dataset/checkpoints/'
BETA = .75
EXP_NAME = f"beta_{BETA}"
files_list = glob.glob(join(TRAIN_PATH))
leng=len(files_list)
new_cover = []
for i in range(leng):
img_cover_path = files_list[i]
for j in range (0,512,16):
for k in range (0,512,16):
img_cover = Image.open(img_cover_path)
area=(k,j,k+16,j+16)
img_cover1=img_cover.crop(area)
img_cover1 = np.array(ImageOps.fit(img_cover1(16,16)),dtype=np.float32)
img_cover1 /= 255.
n1.append(img_cover1)
new_cover.append(n1)
new_cover = np.array(new_cover)
new_cover1=np.swapaxes(new_cover, 1,3)
tf.reset_default_graph()
model=keras.Sequential()
#1st
model.add(keras.layers.Conv2D(64, (3, 3), strides=1,padding='SAME', input_shape = (16, 16, 3072))) #number of filters,shape of filter,input image size,activation function
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation(activation='relu'))
#2
model.add(keras.layers.Conv2D(64, (3, 3),strides=1,padding='SAME')) #number of filters,shape of filter,input image size,activation function
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation(activation='relu'))
#3
model.add(keras.layers.Conv2D(64, (3, 3),strides=1,padding='SAME')) #number of filters,shape of filter,input image size,activation function
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation(activation='relu'))
#4
model.add(keras.layers.Conv2D(64, (3, 3),strides=1,padding='SAME')) #number of filters,shape of filter,input image size,activation function
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation(activation='relu'))
#message
#compiling
model.compile(optimizer = tf.train.AdamOptimizer(0.001),loss='mse', metrics = ['accuracy'])
model.summary()
# Store training stats
model.fit(x=new_cover1,y=None, batch_size=32, epochs=1, verbose=1, callbacks=None, validation_split=0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None)
and it is giving error:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 16, 16, 64) 1769536
_________________________________________________________________
batch_normalization (BatchNo (None, 16, 16, 64) 256
_________________________________________________________________
activation (Activation) (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 16, 16, 64) 36928
_________________________________________________________________
batch_normalization_1 (Batch (None, 16, 16, 64) 256
_________________________________________________________________
activation_1 (Activation) (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 16, 16, 64) 36928
_________________________________________________________________
batch_normalization_2 (Batch (None, 16, 16, 64) 256
_________________________________________________________________
activation_2 (Activation) (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 16, 16, 64) 36928
_________________________________________________________________
batch_normalization_3 (Batch (None, 16, 16, 64) 256
_________________________________________________________________
activation_3 (Activation) (None, 16, 16, 64) 0
=================================================================
Total params: 1,881,344
Trainable params: 1,880,832
Non-trainable params: 512
_________________________________________________________________
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-20-49da746cee1b> in <module>()
24 model.summary()
25 # Store training stats
---> 26 model.fit(x=new_cover1,y=None, batch_size=32, epochs=1, verbose=1, callbacks=None, validation_split=0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None)
27
28 #return model
~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, max_queue_size, workers, use_multiprocessing, **kwargs)
1654 initial_epoch=initial_epoch,
1655 steps_per_epoch=steps_per_epoch,
-> 1656 validation_steps=validation_steps)
1657
1658 def evaluate(self,
~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py in fit_loop(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps)
135 indices_for_conversion_to_dense = []
136 for i in range(len(feed)):
--> 137 if issparse is not None and issparse(ins[i]) and not K.is_sparse(feed[i]):
138 indices_for_conversion_to_dense.append(i)
139
IndexError: list index out of range
回答1:
After my research, it became clearer that this is an already know issue and the official solution has not been released yet. Although, still there are some suggestions claimed to work.
It is suggested to update to the update nightly build version (pip install tf-nightly
or pip install tf-nightly-gpu
)
https://github.com/tensorflow/tensorflow/issues/21894#issuecomment-418552609
回答2:
I think this error is because of the shape of your x and y passed to the model. You passed None as labels!
来源:https://stackoverflow.com/questions/53149386/indexerror-list-index-out-of-range-in-model-fit