示例#1
0
 def parse_example_with_bpp(cls, example):
     return parse_single_example(example,
                                 features={
                                     cls.KW_IMAGE_BYTES:
                                     FixedLenFeature([], dtypes.string),
                                     cls.KW_BPP:
                                     FixedLenFeature([], dtypes.float32)
                                 })
示例#2
0
 def parse_example_with_label(cls, example):
     return parse_single_example(example,
                                 features={
                                     cls.KW_IMAGE_BYTES:
                                     FixedLenFeature([], dtypes.string),
                                     cls.KW_LABEL:
                                     FixedLenFeature([], dtypes.int64)
                                 })
示例#3
0
def build_schema(context_schema, sequence_schema):
    tf_context_schema = {
        feature: FixedLenFeature(shape=attributes['shape'],
                                 dtype=schema_type[attributes['type']])
        for feature, attributes in context_schema.items()
    }
    tf_seq_schema = {
        feature: FixedLenSequenceFeature(shape=attributes['shape'],
                                         dtype=schema_type[attributes['type']])
        for feature, attributes in sequence_schema.items()
    }
    return tf_context_schema, tf_seq_schema
示例#4
0
def decode_object_detection_tf_example(example_proto):
    feature_types = {
        'image/height': FixedLenFeature([], tf.int64),
        'image/width': FixedLenFeature([], tf.int64),
        'image/filename': FixedLenFeature([], tf.string),
        'image/source_id': FixedLenFeature([], tf.string),
        'image/key/sha256': FixedLenFeature([], tf.string),
        'image/encoded': FixedLenFeature([], tf.string),
        'image/format': FixedLenFeature([], tf.string),
        'image/class/labeled_classes': VarLenFeature(tf.int64),
        'image/object/bbox/xmin': VarLenFeature(tf.float32),
        'image/object/bbox/ymin': VarLenFeature(tf.float32),
        'image/object/bbox/xmax': VarLenFeature(tf.float32),
        'image/object/bbox/ymax': VarLenFeature(tf.float32),
        'image/object/class/text': VarLenFeature(tf.string),
        'image/object/class/label': VarLenFeature(tf.int64),
    }

    example = parse_single_example(example_proto, features=feature_types)

    example = {
        'height': example['image/height'],
        'width': example['image/width'],
        'image_filename': example['image/filename'],
        'image_id': example['image/source_id'],
        'key': example['image/key/sha256'],
        'image_bytes': example['image/encoded'],
        'image_filetype': example['image/format'],
        'labeled_classes': example['image/class/labeled_classes'].values,
        'wmin': example['image/object/bbox/xmin'].values,
        'hmin': example['image/object/bbox/ymin'].values,
        'wmax': example['image/object/bbox/xmax'].values,
        'hmax': example['image/object/bbox/ymax'].values,
        'label_names': example['image/object/class/text'].values,
        'label_ids': example['image/object/class/label'].values,
    }

    return example
示例#5
0
predictions = classifier.predict(input_fn=lambda: eval_input_fn(
    predict_x, labels=None, batch_size=BATCH_SIZE))

for prediction, expect in zip(predictions, expected):
    class_id = prediction['class_ids'][0]
    probability = prediction['probabilities'][class_id]
    print('Prediction is "{}" ({:.1f}%), expected "{}"'.format(
        SPECIES[class_id], 100 * probability, expect))

# export model

from tensorflow import FixedLenFeature

feature_specification = {
    'SepalLength':
    FixedLenFeature(shape=(1, ), dtype=tf.float32, default_value=None),
    'SepalWidth':
    FixedLenFeature(shape=(1, ), dtype=tf.float32, default_value=None),
    'PetalLength':
    FixedLenFeature(shape=(1, ), dtype=tf.float32, default_value=None),
    'PetalWidth':
    FixedLenFeature(shape=(1, ), dtype=tf.float32, default_value=None)
}
# feature_spec = tf.feature_column.make_parse_example_spec(feature_columns)

features = {
    'SepalLength': tf.placeholder(dtype=tf.float32,
                                  shape=(1),
                                  name='SepalLength'),
    'SepalWidth': tf.placeholder(dtype=tf.float32,
                                 shape=(1),
示例#6
0
 def parse_example_unlabelled(cls, example):
     return parse_single_example(
         example,
         features={cls.KW_IMAGE_BYTES: FixedLenFeature([], dtypes.string)})
示例#7
0
class KagglePlanetSequence(tf.keras.utils.Sequence):
    """
    Custom Sequence object to train a model on out-of-memory datasets. 
    """
    
    def __init__(self, df, data_path, im_size, batch_size, mode='train'):
        """
        df: pandas dataframe that contains columns with image names and labels
        data_path: path that contains the training images
        im_size: image size
        mode: when in training mode, data will be shuffled between epochs
        """
        self.df = df
        self.batch_size = batch_size
        self.im_size = im_size
        self.mode = mode
        
        # Take labels and a list of image locations in memory
        self.wlabels = self.df['weather_labels'].apply(lambda x: ast.literal_eval(x)).tolist()
        self.glabels = self.df['ground_labels'].apply(lambda x: ast.literal_eval(x)).tolist()
        self.image_list = self.df['image_name'].apply(lambda x: os.path.join(data_path, x + '.jpg')).tolist()
        def __len__(self):
        return int(math.ceil(len(self.df) / float(self.batch_size)))

    def on_epoch_end(self):
        # Shuffles indexes after each epoch
        self.indexes = range(len(self.image_list))
        if self.mode == 'train':
            self.indexes = random.sample(self.indexes, k=len(self.indexes))

    def get_batch_labels(self, idx): 
        # Fetch a batch of labels
        return [self.wlabels[idx * self.batch_size: (idx + 1) * self.batch_size],
                self.glabels[idx * self.batch_size: (idx + 1) * self.batch_size]]

    def get_batch_features(self, idx):
        # Fetch a batch of images
        batch_images = self.image_list[idx * self.batch_size: (1 + idx) * self.batch_size]
        return np.array([load_image(im, self.im_size) for im in batch_images])

    def __getitem__(self, idx):
        batch_x = self.get_batch_features(idx)
        batch_y = self.get_batch_labels(idx)
        return batch_x, batch_y
    
seq = KagglePlanetSequence(df_train,
                       './train-jpg/',
                       im_size=IM_SIZE,
                       batch_size=32)


# In[ ]:


callbacks = [
    tf.keras.callbacks.ModelCheckpoint('./model.h5', verbose=1)
]

model.fit_generator(generator=seq,
                    verbose=1, 
                    epochs=1,
                    use_multiprocessing=False,
                    workers=1,
                    callbacks=callbacks)


# In[ ]:


another_model = tf.keras.models.load_model('./model.h5')
another_model.fit_generator(generator=seq, verbose=1, epochs=1)


# In[ ]:


test_seq = KagglePlanetSequence(df_train,
                       './train-jpg/',
                       im_size=IM_SIZE,
                       batch_size=32,
                       mode='test') # test mode disables shuffling

predictions = model.predict_generator(generator=test_seq, verbose=1)
# We get a list of two prediction arrays, for weather and for label


# In[ ]:


len(predictions[1])  == len(df_train) # Total number of images in dataset


# In[ ]:


# Serialize images, together with labels, to TF records
def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

tf_records_filename = './/KagglePlanetTFRecord_{}'.format(IM_SIZE)
writer = tf.python_io.TFRecordWriter(tf_records_filename)

# List of image paths, np array of labels
im_list = [os.path.join('./train-jpg/', v + '.jpg') for v in df_train['image_name'].tolist()]
w_labels_arr = np.array([ast.literal_eval(l) for l in df_train['weather_labels']])
g_labels_arr = np.array([ast.literal_eval(l) for l in df_train['ground_labels']])

for i in range(len(df_train)):
    w_labels = w_labels_arr[i].astype(np.float32)
    g_labels = g_labels_arr[i].astype(np.float32)
    im = np.array(img_to_array(load_img(im_list[i], target_size=(IM_SIZE, IM_SIZE))) / 255.)
    w_raw = w_labels.tostring()
    g_raw = g_labels.tostring()
    im_raw = im.tostring()
    
    example = tf.train.Example(features=tf.train.Features(feature={'image': _bytes_feature(im_raw),
                                                                  'weather_labels': _bytes_feature(w_raw),
                                                                  'ground_labels': _bytes_feature(g_raw)}))
    
    writer.write(example.SerializeToString())
    
writer.close()


# In[ ]:


from tensorflow import FixedLenFeature
featdef = {
           'image': FixedLenFeature(shape=[], dtype=tf.string),
           'weather_labels': FixedLenFeature(shape=[], dtype=tf.string),
           'ground_labels': FixedLenFeature(shape=[], dtype=tf.string)
          }


# In[ ]:


def _parse_record(example_proto, clip=False):
    ex = tf.parse_single_example(example_proto, featdef)
    
    im = tf.decode_raw(ex['image'], tf.float32)
    im = tf.reshape(im, (IM_SIZE, IM_SIZE, 3))
    
    weather = tf.decode_raw(ex['weather_labels'], tf.float32)
    ground = tf.decode_raw(ex['ground_labels'], tf.float32)
    
    return im, (weather, ground)

# Construct a dataset iterator
batch_size = 32
ds_train = tf.data.TFRecordDataset('./KagglePlanetTFRecord_{}'.format(IM_SIZE)).map(_parse_record)
ds_train = ds_train.repeat().shuffle(1000).batch(batch_size)


# In[ ]:


model = tf.keras.Model(inputs=image_input, outputs=[weather_output, ground_output])

model.compile(optimizer='adam',
              loss={'weather': 'categorical_crossentropy',
                    'ground': 'binary_crossentropy'})

history = model.fit(ds_train, 
                    steps_per_epoch=100, # let's just take some steps
                    epochs=1)


# In[ ]:


import shutil 

tf.keras.backend.clear_session()
tf.keras.backend.set_learning_phase(0)
model = tf.keras.models.load_model('./model.h5')

if os.path.exists('./PlanetModel/1'):
    shutil.rmtree('./PlanetModel/1')
    
export_path = './PlanetModel/1'

# Fetch the Keras session and save the model
with tf.keras.backend.get_session() as sess:
    tf.saved_model.simple_save(
        sess,
        export_path,
        inputs={'input_image': model.input},
        outputs={t.name:t for t in model.outputs})