fchollet / deep-learning-with-python-notebooks

Jupyter notebooks for the code samples of the book "Deep Learning with Python"
MIT License
17.97k stars 8.48k forks source link

Code listing 5.3-using-a-pretrained-convnet, UnboundLocalError: local variable 'batch_outputs' referenced before assignment #147

Open umusa opened 3 years ago

umusa commented 3 years ago

I am trying to run the code list 5.3-using-a-pretrained-convnet on databricks GPU cluster for image understanding by CNN.

The env:

TensorFlow: 2.2 python 3.7.6 keras: 2.3.0-tf Unbuntu: 4.4

The code:

import os
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator

base_dir = '/Users/Downloads/cats_and_dogs_small'

train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
test_dir = os.path.join(base_dir, 'test')

datagen = ImageDataGenerator(rescale=1./255)
batch_size = 20

def extract_features(directory, sample_count):
    features = np.zeros(shape=(sample_count, 4, 4, 512))
    labels = np.zeros(shape=(sample_count))
    generator = datagen.flow_from_directory(
        directory,
        target_size=(150, 150),
        batch_size=batch_size,
        class_mode='binary')
    i = 0
    for inputs_batch, labels_batch in generator:
        features_batch = conv_base.predict(inputs_batch) # error !
        features[i * batch_size : (i + 1) * batch_size] = features_batch
        labels[i * batch_size : (i + 1) * batch_size] = labels_batch
        i += 1
        if i * batch_size >= sample_count:
            # Note that since generators yield data indefinitely in a loop,
            # we must `break` after every image has been seen once.
            break
    return features, labels

train_features, train_labels = extract_features(train_dir, 2000). # error here !
validation_features, validation_labels = extract_features(validation_dir, 1000)
test_features, test_labels = extract_features(test_dir, 1000)

the call stack:

Found 0 images belonging to 0 classes.
UnboundLocalError: local variable 'batch_outputs' referenced before assignment
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<command-2528158> in <module>
     33     return features, labels
     34 
---> 35 train_features, train_labels = extract_features(train_dir, 2000)
     36 validation_features, validation_labels = extract_features(validation_dir, 1000)
     37 test_features, test_labels = extract_features(test_dir, 1000)

<command-2528158> in extract_features(directory, sample_count)
     23     i = 0
     24     for inputs_batch, labels_batch in generator:
---> 25         features_batch = conv_base.predict(inputs_batch)
     26         features[i * batch_size : (i + 1) * batch_size] = features_batch
     27         labels[i * batch_size : (i + 1) * batch_size] = labels_batch

/databricks/python/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
     86       raise ValueError('{} is not supported in multi-worker mode.'.format(
     87           method.__name__))
---> 88     return method(self, *args, **kwargs)
     89 
     90   return tf_decorator.make_decorator(

/databricks/python/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
   1283             callbacks.on_predict_batch_end(step, {'outputs': batch_outputs})
   1284       callbacks.on_predict_end()
-> 1285     all_outputs = nest.map_structure_up_to(batch_outputs, concat, outputs)
   1286     return tf_utils.to_numpy_or_python_type(all_outputs)
   1287 

UnboundLocalError: local variable 'batch_outputs' referenced before assignment
reachpranjal commented 3 years ago

I have created a pull request regarding the same. The code is running successfully now.