GithubHelp home page GithubHelp logo

arsey / keras-transfer-learning-for-oxford102 Goto Github PK

View Code? Open in Web Editor NEW
270.0 13.0 109.0 39.98 MB

Keras pretrained models (VGG16, InceptionV3, Resnet50, Resnet152) + Transfer Learning for predicting classes in the Oxford 102 flower dataset

License: MIT License

Python 100.00%

keras-transfer-learning-for-oxford102's Introduction

See my application for identifying plants - Plant Care. It works using the code from the model implemented in this repo.

Keras pretrained models (VGG16, InceptionV3, Resnet50, Resnet152) + Transfer Learning for predicting classes in the Oxford 102 flower dataset (or any custom dataset)

This bootstraps the training of deep convolutional neural networks with Keras to classify images in the Oxford 102 category flower dataset.

Train process is fully automated and the best weights will be saved.

The code can be used for any dataset, just follow the original files structure in data/sorted directory after running bootstrap.py. If you want to store your dataset somewhere else, you can do it and run train.py with setting a path to dataset with a special parameter --data_dir==/full/path/to/your/sorted/data

Dataset directory's structure

Dataset directory's structure

Notice: for ResNet152 you should download weights manually here and put them under the project_directory/weights

Overview

  • bootstrap.py: downloads the Oxford 102 dataset and prepare image files
  • train.py: starts end-to-end training process
  • server.py: a small python server based on sockets and designed to keep a model in memory for fast recognition requests
  • client.py: a client that sends requests to server.py

Usage

Step 1: Bootstrap

python bootstrap.py

Step 2: Train

python train.py --model=resnet50

Step 3: Get predictions using predict.py or server.py + client.py

Using predict.py:

python predict.py --path "/full/path/to/image" --model=resnet50

Using server.py + client.py:

  1. run server and wait till model is loaded. Do not break server, it should be run and listen for incoming connections
python server.py --model=resnet50
  1. send requests using client
python client.py --path "/full/path/to/image"

keras-transfer-learning-for-oxford102's People

Contributors

arsey avatar jimgoo avatar sakares avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

keras-transfer-learning-for-oxford102's Issues

Imagenet mean subtraction

Is it not necessecary to include the argument featurewise_center=True when creating ImageDataGenerator for the imagenet mean to get subtracted? (specifically on line 15 in bottleneck.py, and also in fine_tuning.py)

When i look at the keras source code for flow_from_directory they only seem to subtract the mean from images when featurewise_center is set to True.

Request for enhancement

Please answer the questions before you submit your issue.

  • backend = TensorFlow 1.2.1

  • Python version = 3.5

  • Keras version = 2.0.6

Whe I request a prediction for a batch of 10 images like:

python predict.py --path "data/sorted/test/5/" --model=resnet50 --batch_size=10

I get a response like:
| should be image_07165.jpg (data/sorted/test/5) -> predicted as 47.0 (5)
| should be image_07169.jpg (data/sorted/test/5) -> predicted as 47.0 (5)
| should be image_07178.jpg (data/sorted/test/5) -> predicted as 47.0 (5)
| should be image_07191.jpg (data/sorted/test/5) -> predicted as 76.0 (76)
| should be image_07192.jpg (data/sorted/test/5) -> predicted as 47.0 (5)
| should be image_07194.jpg (data/sorted/test/5) -> predicted as 23.0 (28)
| should be image_07195.jpg (data/sorted/test/5) -> predicted as 28.0 (32)
| should be image_08105.jpg (data/sorted/test/5) -> predicted as 28.0 (32)
| should be image_08108.jpg (data/sorted/test/5) -> predicted as 47.0 (5)
| should be image_08109.jpg (data/sorted/test/5) -> predicted as 69.0 (7)

Can this response be more meaningful as:

image_07165.jpg (data/sorted/test/5) -> predicted with 47.0% confidence that it is category 5 (Tiger Lilly)
image_07191.jpg (data/sorted/test/5) -> predicted with 76.0 % confidence that it is category 76 (passion flower)
image_07194.jpg (data/sorted/test/5) -> predicted with 23.0 % confidence that it is category 28(love in the mist)
image_07195.jpg (data/sorted/test/5) -> predicted with 28.0 % confidence that it is category 32(bird of paradise)

Thank you

Same problem with fine tuning.

Your issue is that I'm trying to adopt this approach for multiclass classification. And the first step when we're training the top model from bottlenecks works fine and I have 76% accuracy for 102 classes. But when I proceed to fine-tune step, the model with each next epoch decreases in accuracy. So if at the end of 1st epoch of fine-tune I have about 45% of accuracy, on the 2nd epoch I'm getting 40%, etc.

Same problem with fine tuning. Im trying now and model decreased in accuracy from 70% to 40%.
I'm trying to find the issue for a few weeks but no results. Whether you have resolved it,thanks!

Can we transfer learning for any generic image set with multiple classes

Hi,

This works awesome for oxford102 flowers DB, but if you can modify this for working on any DB with proper structured imageset then it become super awesome.

Would really love to know your views on that. If you don't have time, then you can let me know the steps and I can give a PR for this.

Error in weights file I guess...I am at a loss

ValueError: Input dimension mis-match. (input[0].shape[1] = 7, input[1].shape[1] = 64)

Here is the trace:

Creating model...
C:\Users\shiri\Documents\keras-transfer-learning-for-oxford102-master\models\resnet50.py:33: UserWarning: Update your `Model` call to the Keras 2 API: `Model(outputs=Softmax.0, inputs=/input_1)`
  self.model = Model(input=base_model.input, output=predictions)
Model is created
Fine tuning...
Freezing 80 layers
Found 6149 images belonging to 102 classes.
Found 1020 images belonging to 102 classes.
./models\base_model.py:43: UserWarning: Update your `fit_generator` call to the Keras 2 API: `fit_generator(<keras.pre..., validation_steps=1020, callbacks=[<keras.ca..., epochs=1000, steps_per_epoch=192, validation_data=<keras.pre..., class_weight={0: 2.4765...)`
  class_weight=self.class_weight)
Epoch 1/1000
Input dimension mis-match. (input[0].shape[1] = 7, input[1].shape[1] = 64)
Apply node that caused the error: Elemwise{add,no_inplace}(AbstractConv2d{convdim=2, border_mode='valid', subsample=(2, 2), filter_flip=True, imshp=(None, 3, 230, 230), kshp=(64, 3, 7, 7), filter_dilation=(1, 1)}.0, Reshape{4}.0)
Toposort index: 3158
Inputs types: [TensorType(float32, 4D), TensorType(float32, (True, False, True, True))]
Inputs shapes: [(32, 7, 114, 84), (1, 64, 1, 1)]
Inputs strides: [(1066128, 152304, 1336, 8), (256, 4, 4, 4)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[AbstractBatchNormInference{axes=(0, 2, 3)}(Elemwise{add,no_inplace}.0, Reshape{4}.0, Reshape{4}.0, Reshape{4}.0, Reshape{4}.0, TensorConstant{0.00100000..0474974513}), AbstractBatchNormTrain{axes=(0, 2, 3)}(Elemwise{add,no_inplace}.0, InplaceDimShuffle{x,0,x,x}.0, InplaceDimShuffle{x,0,x,x}.0, TensorConstant{0.00100000..0474974513}, TensorConstant{0.10000000149011612})]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
  File "C:\Anaconda3\envs\py35\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "C:/Users/shiri/Documents/keras-transfer-learning-for-oxford102-master/train.py", line 50, in <module>
    model.train()
  File "./models\base_model.py", line 48, in train
    self._create()
  File "C:\Users\shiri\Documents\keras-transfer-learning-for-oxford102-master\models\resnet50.py", line 21, in _create
    base_model = KerasResNet50(include_top=False, input_tensor=self.get_input_tensor())
  File "C:\Anaconda3\envs\py35\lib\site-packages\keras\applications\resnet50.py", line 207, in ResNet50
    x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
  File "C:\Anaconda3\envs\py35\lib\site-packages\keras\engine\topology.py", line 554, in __call__
    output = self.call(inputs, **kwargs)
  File "C:\Anaconda3\envs\py35\lib\site-packages\keras\layers\convolutional.py", line 178, in call
    data_format=self.data_format)
  File "C:\Anaconda3\envs\py35\lib\site-packages\keras\backend\theano_backend.py", line 1938, in bias_add
    x += reshape(bias, (1, bias.shape[0], 1, 1))

HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
Traceback (most recent call last):
  File "C:\Anaconda3\envs\py35\lib\site-packages\theano\compile\function_module.py", line 884, in __call__
    self.fn() if output_subset is None else\
ValueError: Input dimension mis-match. (input[0].shape[1] = 7, input[1].shape[1] = 64)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/shiri/Documents/keras-transfer-learning-for-oxford102-master/train.py", line 50, in <module>
    model.train()
  File "./models\base_model.py", line 51, in train
    self._fine_tuning()
  File "./models\base_model.py", line 43, in _fine_tuning
    class_weight=self.class_weight)
  File "C:\Anaconda3\envs\py35\lib\site-packages\keras\legacy\interfaces.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "C:\Anaconda3\envs\py35\lib\site-packages\keras\engine\training.py", line 1876, in fit_generator
    class_weight=class_weight)
  File "C:\Anaconda3\envs\py35\lib\site-packages\keras\engine\training.py", line 1620, in train_on_batch
    outputs = self.train_function(ins)
  File "C:\Anaconda3\envs\py35\lib\site-packages\keras\backend\theano_backend.py", line 1094, in __call__
    return self.function(*inputs)
  File "C:\Anaconda3\envs\py35\lib\site-packages\theano\compile\function_module.py", line 898, in __call__
    storage_map=getattr(self.fn, 'storage_map', None))
  File "C:\Anaconda3\envs\py35\lib\site-packages\theano\gof\link.py", line 325, in raise_with_op
    reraise(exc_type, exc_value, exc_trace)
  File "C:\Anaconda3\envs\py35\lib\site-packages\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Anaconda3\envs\py35\lib\site-packages\theano\compile\function_module.py", line 884, in __call__
    self.fn() if output_subset is None else\
ValueError: Input dimension mis-match. (input[0].shape[1] = 7, input[1].shape[1] = 64)
Apply node that caused the error: Elemwise{add,no_inplace}(AbstractConv2d{convdim=2, border_mode='valid', subsample=(2, 2), filter_flip=True, imshp=(None, 3, 230, 230), kshp=(64, 3, 7, 7), filter_dilation=(1, 1)}.0, Reshape{4}.0)
Toposort index: 3158
Inputs types: [TensorType(float32, 4D), TensorType(float32, (True, False, True, True))]
Inputs shapes: [(32, 7, 114, 84), (1, 64, 1, 1)]
Inputs strides: [(1066128, 152304, 1336, 8), (256, 4, 4, 4)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[AbstractBatchNormInference{axes=(0, 2, 3)}(Elemwise{add,no_inplace}.0, Reshape{4}.0, Reshape{4}.0, Reshape{4}.0, Reshape{4}.0, TensorConstant{0.00100000..0474974513}), AbstractBatchNormTrain{axes=(0, 2, 3)}(Elemwise{add,no_inplace}.0, InplaceDimShuffle{x,0,x,x}.0, InplaceDimShuffle{x,0,x,x}.0, TensorConstant{0.00100000..0474974513}, TensorConstant{0.10000000149011612})]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
  File "C:\Anaconda3\envs\py35\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "C:/Users/shiri/Documents/keras-transfer-learning-for-oxford102-master/train.py", line 50, in <module>
    model.train()
  File "./models\base_model.py", line 48, in train
    self._create()
  File "C:\Users\shiri\Documents\keras-transfer-learning-for-oxford102-master\models\resnet50.py", line 21, in _create
    base_model = KerasResNet50(include_top=False, input_tensor=self.get_input_tensor())
  File "C:\Anaconda3\envs\py35\lib\site-packages\keras\applications\resnet50.py", line 207, in ResNet50
    x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
  File "C:\Anaconda3\envs\py35\lib\site-packages\keras\engine\topology.py", line 554, in __call__
    output = self.call(inputs, **kwargs)
  File "C:\Anaconda3\envs\py35\lib\site-packages\keras\layers\convolutional.py", line 178, in call
    data_format=self.data_format)
  File "C:\Anaconda3\envs\py35\lib\site-packages\keras\backend\theano_backend.py", line 1938, in bias_add
    x += reshape(bias, (1, bias.shape[0], 1, 1))

HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

finetuning multiclass model

Hi Arsey,

I am fine tuning a multiclass problem (class 8)
My code is below:

top_model.load_weights(top_model_weights_path)

add the model on top of the convolutional base

model.add(top_model)

set the first 25 layers (up to the last conv block)

to non-trainable (weights will not be updated)

for layer in model.layers[:25]:
layer.trainable = False

compile the model with a SGD/momentum optimizer

and a very slow learning rate.

model.compile(loss='sparse_categorical_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])

prepare data augmentation configuration

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_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=32,
class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=32,
class_mode='categorical')

fine-tune the model

model.fit_generator(
train_generator,
samples_per_epoch=nb_train_samples,
nb_epoch=nb_epoch,
validation_data=validation_generator,
nb_val_samples=nb_validation_samples)

I get the following error:
Traceback (most recent call last):
File "prog.py", line 131, in
nb_val_samples=nb_validation_samples)
File "C:\Users\bgsingh\Anaconda2\lib\site-packages\keras\models.py", line 935, in fit_generator
initial_epoch=initial_epoch)
File "C:\Users\bgsingh\Anaconda2\lib\site-packages\keras\engine\training.py", line 1553, in fit_generator
class_weight=class_weight)
File "C:\Users\bgsingh\Anaconda2\lib\site-packages\keras\engine\training.py", line 1310, in train_on_batch
check_batch_axis=True)
File "C:\Users\bgsingh\Anaconda2\lib\site-packages\keras\engine\training.py", line 1034, in _standardize_user_data
exception_prefix='model target')
File "C:\Users\bgsingh\Anaconda2\lib\site-packages\keras\engine\training.py", line 124, in standardize_input_data
str(array.shape))
ValueError: Error when checking model target: expected sequential_2 to have shape (None, 1) but got array with shape (32L, 8L)

I guess the the generator takes care of the label?

Dataset directory's structure

I wants to train my own datasets about 100 classes ,so I want to know the structures of datasets,and some other guide,Thanks!

cannot download vgg16_tf_dim_ordering_notop.h5 file.

Hi Arsey,
When I typed "python train.py" , this problem arisen that connect failed to download above weight file.
So I tried to download it from github and place it under the same path with vgg16 models, but failed downloading again, any advices?
thank you

dict().values returns dict_values in python 3.6

When calling the .values method on a dict() object in python 3.6, a dict_values is returned, not a list as was the case in earlier versions.

This causes an error in the vis.utils.get_class_weight method in python 3.6. To fix this, you need to change

total = np.sum(class_number.values())
max_samples = np.max(class_number.values())

to

total = np.sum(list(class_number.values()))
max_samples = np.max(list(class_number.values()))

My understanding is that this patch shouldn't affect 2.* versions of python.

me again. Keyerror. "Unable to open object (Object 'fc1' doesn't exist)"

File "train.py", line 39, in <module> fine_tuning.tune() File "/home/slz/Desktop/myvgg/fine_tuning.py", line 10, in tune model = util.load_model(nb_class=len(config.classes), weights_path=config.top_model_weights_path) File "/home/slz/Desktop/myvgg/util.py", line 130, in load_model output=base_model.output) File "/home/slz/Desktop/myvgg/util.py", line 89, in get_top_model_for_VGG16 weights_1 = get_layer_weights(weights_file, 'fc1') File "/home/slz/Desktop/myvgg/util.py", line 67, in get_layer_weights g = weights_file[layer_name] File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/tmp/pip_build_root/h5py/h5py/_objects.c:2841) File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/tmp/pip_build_root/h5py/h5py/_objects.c:2799) File "/usr/local/lib/python2.7/dist-packages/h5py/_hl/group.py", line 166, in __getitem__ oid = h5o.open(self.id, self._e(name), lapl=self._lapl) File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/tmp/pip_build_root/h5py/h5py/_objects.c:2841) File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/tmp/pip_build_root/h5py/h5py/_objects.c:2799) File "h5py/h5o.pyx", line 190, in h5py.h5o.open (/tmp/pip_build_root/h5py/h5py/h5o.c:3727) KeyError: "Unable to open object (Object 'fc1' doesn't exist)"

Why we have to rescale by 1. / 255

train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
horizontal_flip=True)

I saw this process in Keras blog, and your implementation. But I don't understand why?


def preprocess_input(x, dim_ordering='default'):
    if dim_ordering == 'default':
        dim_ordering = K.image_dim_ordering()
    assert dim_ordering in {'tf', 'th'}

    if dim_ordering == 'th':
        x[:, 0, :, :] -= 103.939
        x[:, 1, :, :] -= 116.779
        x[:, 2, :, :] -= 123.68
        # 'RGB'->'BGR'
        x = x[:, ::-1, :, :]
    else:
        x[:, :, :, 0] -= 103.939
        x[:, :, :, 1] -= 116.779
        x[:, :, :, 2] -= 123.68
        # 'RGB'->'BGR'
        x = x[:, :, :, ::-1]
return x

this is preprocess step on imagenet_util.py, It just minus the mean value of each dimension.

System calls classification

Hello, I'm trying to do task for system calls classification. The code bellow is inspired from a text classification project. My system calls are represented as sequences of integers between 1 and 340. The error I got is: valueError: input arrays should have the same number of samples as target arrays. Find 1 input samples and 0 target samples.
I don't know how to resolve it !!

`

       df = pd.read_csv("data.txt") 
       df_test = pd.read_csv("validation.txt")
      #split arrays into train and test data (cross validation)
        train_text, test_text, train_y, test_y = train_test_split(df,df,test_size = 0.2)
    MAX_NB_WORDS = 5700
   # get the raw text data
    texts_train = train_text.astype(str)
    texts_test = test_text.astype(str)
    # finally, vectorize the text samples into a 2D integer tensor
   tokenizer = Tokenizer(nb_words=MAX_NB_WORDS, char_level=False)
   tokenizer.fit_on_texts(texts_train)
   sequences = tokenizer.texts_to_sequences(texts_train)
   sequences_test = tokenizer.texts_to_sequences(texts_test)

  word_index = tokenizer.word_index
  type(tokenizer.word_index), len(tokenizer.word_index)
  index_to_word = dict((i, w) for w, i in tokenizer.word_index.items()) 
 " ".join([index_to_word[i] for i in sequences[0]])
  seq_lens = [len(s) for s in sequences]

  MAX_SEQUENCE_LENGTH = 100
 # pad sequences with 0s
 x_train = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH) 
 x_test = pad_sequences(sequences_test, maxlen=MAX_SEQUENCE_LENGTH)
 #print('Shape of data train:', x_train.shape)  #cela a donnée (1,100)
 #print('Shape of data test tensor:', x_test.shape)
 y_train = train_y
 y_test = test_y
 print('Shape of label tensor:', y_train.shape)
 EMBEDDING_DIM = 32
 N_CLASSES = 2

y_train = keras.utils.to_categorical( y_train , N_CLASSES )
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='float32')

embedding_layer = Embedding(MAX_NB_WORDS, EMBEDDING_DIM,
                        input_length=MAX_SEQUENCE_LENGTH,
                        trainable=True)
embedded_sequences = embedding_layer(sequence_input)

average = GlobalAveragePooling1D()(embedded_sequences)
 predictions = Dense(N_CLASSES, activation='softmax')(average)

 model = Model(sequence_input, predictions)
 model.compile(loss='categorical_crossentropy',
          optimizer='adam', metrics=['acc'])
 model.fit(x_train, y_train, validation_split=0.1,
      nb_epoch=10, batch_size=1)
  output_test = model.predict(x_test)
  print("test auc:", roc_auc_score(y_test,output_test[:,1]))

`

What does the '[103.939, 116.779, 123.68]' means?

  • Which backend do you use (TensorFlow or Theano) and what its version?
    TensorFlow
  • What version of Python?
    3.6.4
  • What version of Keras?
    2.2.4

Thank you for your working firstly.
I have a question about the xxx.mean = np.array([103.939, 116.779, 123.68], dtype=np.float32).reshape((3, 1, 1))

What does the '[103.939, 116.779, 123.68]' means?
Does it means the datagen should reduce the imagenet's BGR mean value?

Looking forward to your reply. Thank you very much!

Issue with running predict.py file

Hi,
Hope you are doing great. First of all, I would not able to reach you through email(showing incorrect email address). I have tried to run your code, the following files are running properly : bootstrap.py and train.py ( database, training and validation)
I would not able to run predict.py file. Could you please let me know what does "path/to/image' mean?
I used testing directory path according the following commands:
python predict.py -p "/home/....../sorted/test/" --model=vgg16
showing syntax errors..
could you please give me solution regarding this issue?
Thanks
zahangir

How can I avoid "early stopping"?

Hello, I'm trying to use your code to create my image classification model.

When I run train.py, I get an "early stopping" message early on and end train.py.

Epoch 26/100 1/70 [..............................] - ETA: 12s - loss: 0.3207 - accuracy: 0.906 2/70 [..............................] - ETA: 12s - loss: 0.4309 - accuracy: 0.859 3/70 [>.............................] - ETA: 11s - loss: 0.4152 - accuracy: 0.864 4/70 [>.............................] - ETA: 11s - loss: 0.3709 - accuracy: 0.882 5/70 [=>............................] - ETA: 11s - loss: 0.3812 - accuracy: 0.856 6/70 [=>............................] - ETA: 11s - loss: 0.3986 - accuracy: 0.849 7/70 [==>...........................] - ETA: 11s - loss: 0.4186 - accuracy: 0.839 8/70 [==>...........................] - ETA: 11s - loss: 0.4204 - accuracy: 0.839 9/70 [==>...........................] - ETA: 10s - loss: 0.4238 - accuracy: 0.84310/70 [===>..........................] - ETA: 10s - loss: 0.4284 - accuracy: 0.84311/70 [===>..........................] - ETA: 10s - loss: 0.4258 - accuracy: 0.84912/70 [====>.........................] - ETA: 10s - loss: 0.4850 - accuracy: 0.83813/70 [====>.........................] - ETA: 10s - loss: 0.4736 - accuracy: 0.83614/70 [=====>........................] - ETA: 9s - loss: 0.4740 - accuracy: 0.834870/70 [==============================] - 15s 220ms/step - loss: 0.5117 - accuracy: 0.8263 - val_loss: 1.9986 - val_accuracy: 0.3203 Epoch 27/100 1/70 [..............................] - ETA: 12s - loss: 0.6101 - accuracy: 0.750 2/70 [..............................] - ETA: 12s - loss: 0.5081 - accuracy: 0.812 3/70 [>.............................] - ETA: 11s - loss: 0.4593 - accuracy: 0.833 4/70 [>.............................] - ETA: 11s - loss: 0.4932 - accuracy: 0.828 5/70 [=>............................] - ETA: 11s - loss: 0.5176 - accuracy: 0.825 6/70 [=>............................] - ETA: 11s - loss: 0.4593 - accuracy: 0.838 7/70 [==>...........................] - ETA: 11s - loss: 0.5008 - accuracy: 0.825 8/70 [==>...........................] - ETA: 10s - loss: 0.4992 - accuracy: 0.828 9/70 [==>...........................] - ETA: 10s - loss: 0.4836 - accuracy: 0.83310/70 [===>..........................] - ETA: 10s - loss: 0.5110 - accuracy: 0.82811/70 [===>..........................] - ETA: 10s - loss: 0.5570 - accuracy: 0.81212/70 [====>.........................] - ETA: 10s - loss: 0.5605 - accuracy: 0.80713/70 [====>.........................] - ETA: 10s - loss: 0.5754 - accuracy: 0.80514/70 [=====>........................] - ETA: 9s - loss: 0.5727 - accuracy: 0.810370/70 [==============================] - 15s 216ms/step - loss: 0.5514 - accuracy: 0.8138 - val_loss: 2.2937 - val_accuracy: 0.3672 Epoch 28/100 1/70 [..............................] - ETA: 12s - loss: 0.7783 - accuracy: 0.812 2/70 [..............................] - ETA: 12s - loss: 0.8480 - accuracy: 0.765 3/70 [>.............................] - ETA: 11s - loss: 0.6885 - accuracy: 0.791 4/70 [>.............................] - ETA: 11s - loss: 0.6202 - accuracy: 0.828 5/70 [=>............................] - ETA: 11s - loss: 0.5674 - accuracy: 0.843 6/70 [=>............................] - ETA: 11s - loss: 0.5644 - accuracy: 0.843 7/70 [==>...........................] - ETA: 11s - loss: 0.5498 - accuracy: 0.848 8/70 [==>...........................] - ETA: 11s - loss: 0.5700 - accuracy: 0.835 9/70 [==>...........................] - ETA: 10s - loss: 0.5517 - accuracy: 0.83610/70 [===>..........................] - ETA: 10s - loss: 0.5372 - accuracy: 0.84011/70 [===>..........................] - ETA: 10s - loss: 0.5292 - accuracy: 0.84012/70 [====>.........................] - ETA: 10s - loss: 0.5308 - accuracy: 0.84113/70 [====>.........................] - ETA: 10s - loss: 0.5164 - accuracy: 0.84614/70 [=====>........................] - ETA: 9s - loss: 0.5274 - accuracy: 0.843870/70 [==============================] - 15s 217ms/step - loss: 0.5307 - accuracy: 0.8201 - val_loss: 1.9304 - val_accuracy: 0.3141 Epoch 00028: early stopping Classes are saved Training is finished!
I checked the values of "-loss", "-accuracy", "-val_loss" and "-val_accuracy", and I thought that I should still continue train.py.
I set 100 with "--nb_epoch". How can I keep the train up to 100 epoch?

The versions such as Tensorflow installed on my PC are:
・Python : 3.6
・CUDA : 10.0
・cuDNN : 7.6.5
・Tensorflow-gpu : 1.15
・keras : 2.3.1

Thank you for your cooperation!

Value error: input samples have different number of arrays with target samples.

Hi Arsey,
After I get the bottleneck features of oxford102 dataset, and go to use these features to train a small fully-connected model (bottlneck.py/ train_top_model() ), I got a value error:

Using Theano backend.
K.image_dim_ordering: th
Found 6149 images belonging to 102 classes.
Found 1020 images belonging to 102 classes.
Traceback (most recent call last):
File "train.py", line 38, in
bottlenecks.train_top_model()
File "/home/smher/Desktop/ML_Work/myVGG16_v1/bottlenecks.py", line 61, in train_top_model
callbacks=callbacks_list)
File "/home/smher/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1068, in fit
batch_size=batch_size)
File "/home/smher/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 993, in _standardize_user_data
check_array_lengths(x, y, sample_weights)
File "/home/smher/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 182, in check_array_lengths
'and ' + str(list(set_y)[0]) + ' target samples.')
ValueError: Input arrays should have the same number of samples as target arrays. Found 6149 input samples and 8660 target samples.

How can I fix this error ? Any advices ?

ValueError: Dimension 1 in both shapes must be equal, but are 0 and 100 for 'Assign_320' (op: 'Assign') with input shapes: [2048,0], [2048,100].

/home/deeplearning/下载/baidu_dogs/keras-transfer-learning-for-oxford102/models/resnet50.py:32: UserWarning: Update your `Model` call to the Keras 2 API: `Model(outputs=Tensor("pr..., inputs=Tensor("in...)`
  self.model = Model(input=base_model.input, output=predictions)
Traceback (most recent call last):
  File "/home/deeplearning/下载/pycharm-community-2017.1.4/helpers/pydev/pydevd.py", line 1591, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/home/deeplearning/下载/pycharm-community-2017.1.4/helpers/pydev/pydevd.py", line 1018, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/deeplearning/下载/baidu_dogs/keras-transfer-learning-for-oxford102/predict.py", line 144, in <module>
    model = model_module.load()
  File "/home/deeplearning/下载/baidu_dogs/keras-transfer-learning-for-oxford102/models/base_model.py", line 58, in load
    self.model.load_weights(config.get_fine_tuned_weights_path())
  File "/home/deeplearning/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 2572, in load_weights
    load_weights_from_hdf5_group(f, self.layers)
  File "/home/deeplearning/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 3008, in load_weights_from_hdf5_group
    K.batch_set_value(weight_value_tuples)
  File "/home/deeplearning/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2184, in batch_set_value
    assign_op = x.assign(assign_placeholder)
  File "/home/deeplearning/.local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 516, in assign
    return state_ops.assign(self._variable, value, use_locking=use_locking)
  File "/home/deeplearning/.local/lib/python2.7/site-packages/tensorflow/python/ops/state_ops.py", line 271, in assign
    validate_shape=validate_shape)
  File "/home/deeplearning/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_state_ops.py", line 45, in assign
    use_locking=use_locking, name=name)
  File "/home/deeplearning/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/home/deeplearning/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2508, in create_op
    set_shapes_for_outputs(ret)
  File "/home/deeplearning/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1873, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/home/deeplearning/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1823, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "/home/deeplearning/.local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
    debug_python_shape_fn, require_shape_fn)
  File "/home/deeplearning/.local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Dimension 1 in both shapes must be equal, but are 0 and 100 for 'Assign_320' (op: 'Assign') with input shapes: [2048,0], [2048,100].

I have modified the code with the tf backend and change the inputshape to fit it, but when I wanted to predit the classes using the predict.py, I got the error, can anyone help me?

I didn't change any code in predict.py

server.py ValueError

It looks like predict.py is working.
-------------------------- pythin predict.ty --path "" --model=resnet50
Creating model
Found 1 files
Batch 0
Warming up the model
Warming up took 31.234445 s
Prediction on batch 0 took: 0.306215
| should be 48 (50) -> predicted as 48.0 (50)

however server.py faced following error ...
$python server.py --model=resnet50
Using Theano backend.
WARNING (theano.sandbox.cuda): The cuda backend is deprecated and will be removed in the next release (v0.10). Please switch to the gpuarray backend. You can get more information about how to switch at this URL:
https://github.com/Theano/Theano/wiki/Converting-to-the-new-gpu-back-end%28gpuarray%29

Using gpu device 0: Tesla K80 (CNMeM is disabled, cuDNN not available)
Creating model
(Subtensor{int64}.0, Elemwise{add,no_inplace}.0, Elemwise{add,no_inplace}.0, Subtensor{int64}.0)
Traceback (most recent call last):
File "server.py", line 21, in
model = model_module.load()
File "/home/norihisa_ando/oxford/keras-transfer-learning-for-oxford102/models/base_model.py", line 56, in load
self._create()
File "/home/norihisa_ando/oxford/keras-transfer-learning-for-oxford102/models/resnet50.py", line 24, in _create
x = Flatten()(x)
File "/home/norihisa_ando/.pyenv/versions/2.7.3/lib/python2.7/site-packages/keras/engine/topology.py", line 569, in call
self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
File "/home/norihisa_ando/.pyenv/versions/2.7.3/lib/python2.7/site-packages/keras/engine/topology.py", line 632, in add_inbound_node
Node.create_node(self, inbound_layers, node_indices, tensor_indices)
File "/home/norihisa_ando/.pyenv/versions/2.7.3/lib/python2.7/site-packages/keras/engine/topology.py", line 168, in create_node
output_shapes = to_list(outbound_layer.get_output_shape_for(input_shapes[0]))
File "/home/norihisa_ando/.pyenv/versions/2.7.3/lib/python2.7/site-packages/keras/layers/core.py", line 436, in get_output_shape_for
'(got ' + str(input_shape[1:]) + '. '
ValueError: The shape of the input to "Flatten" is not fully defined (got (0, 1, 2048). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.

image_dim_ordering is "th"
predict.py:keras.backend.set_image_dim_ordering('th')
train.py:keras.backend.set_image_dim_ordering('th')

SOLVED.

I just add in server.py ..... it is now woring ... ( I realized this solution when I am writing issue ... thanks)
os.environ["THEANO_FLAGS"] = "lib.cnmem=2"
keras.backend.set_image_dim_ordering('th')

predict.py fails on vgg16

I have fine-tunned vgg16 model. Then I wanted to run predict.py, I got this:

Called with args: Namespace(accuracy=False, batch_size=500, data_dir=None, execution_time=False, model='vgg16', novelty_detection=False, path='/home/xdvx/deeplearningtools/nudity/negative/', plot_confusion_matrix=False, store_activations=False) Creating model Traceback (most recent call last): File "predict.py", line 142, in <module> model = model_module.load() File "/home/xdvx/deeplearningtools/keras-transfer-learning-for-oxford102/models/base_model.py", line 56, in load self._create() File "/home/xdvx/deeplearningtools/keras-transfer-learning-for-oxford102/models/vgg16.py", line 16, in _create base_model = KerasVGG16(weights='imagenet', include_top=False, input_tensor=self.get_input_tensor()) File "/usr/local/lib/python2.7/dist-packages/keras/applications/vgg16.py", line 118, in VGG16 x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 596, in __call__ output = self.call(inputs, **kwargs) File "/usr/local/lib/python2.7/dist-packages/keras/layers/pooling.py", line 154, in call data_format=self.data_format) File "/usr/local/lib/python2.7/dist-packages/keras/layers/pooling.py", line 217, in _pooling_function pool_mode='max') File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 3288, in pool2d x = tf.nn.max_pool(x, pool_size, strides, padding=padding) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_ops.py", line 1839, in max_pool name=name) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 1672, in _max_pool data_format=data_format, name=name) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op op_def=op_def) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2338, in create_op set_shapes_for_outputs(ret) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1719, in set_shapes_for_outputs shapes = shape_func(op) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1669, in call_with_requiring return call_cpp_shape_fn(op, require_shape_fn=True) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn debug_python_shape_fn, require_shape_fn) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl raise ValueError(err.message) ValueError: Negative dimension size caused by subtracting 2 from 1 for 'block2_pool/MaxPool' (op: 'MaxPool') with input shapes: [?,1,112,128].

train error:ValueError: Input arrays should have the same number of samples as target arrays

Input arrays should have the same number of samples as target arrays. Found 8837 input samples and 297 target samples.
Traceback (most recent call last):
File "train.py", line 39, in
model_module.train(class_weight=class_weight)
File "/media/hlgdeep/_data/keras_project/flower-oxford102/models/vgg16.py", line 216, in train
train_top_model(class_weight=class_weight)
File "/media/hlgdeep/_data/keras_project/flower-oxford102/models/vgg16.py", line 146, in train_top_model
class_weight=class_weight)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1406, in fit
batch_size=batch_size)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1308, in _standardize_user_data
_check_array_lengths(x, y, sample_weights)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 229, in _check_array_lengths
'and ' + str(list(set_y)[0]) + ' target samples.')
ValueError: Input arrays should have the same number of samples as target arrays. Found 8837 input samples and 297 target samples.

Pretrained model

Hi, Alex

First of all, thank you for your work and for sharing with us! :)

I found that this repo contained the link to pre-trained models
https://github.com/Arsey/keras-transfer-learning-for-oxford102/pull/23/files#diff-c9bfeb27d1153a74dbaec8124d755317 by @indiejoseph.

Unfortunately, they are not available now. Is it possible to download somewhere pre-trained models, classes, etc. (/trained folder)?
Because without a good hardware train.py takes a lot of time. Would be great to skip the step 2

Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.