Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Deep Learning Hot Dog Detector

Deep Learning Hot Dog Detector

How to build a hot dog detection neural network using semi-supervised training and transfer learning.

Leszek Rybicki

July 17, 2018
Tweet

More Decks by Leszek Rybicki

Other Decks in Programming

Transcript

  1. ?

  2. © Alex Kendall et al. Multi-Task Learning Using Uncertainty to

    Weigh Losses or Scene Geometry and Semantics, 2018
  3. IMG_SIZE=[224, 224] import keras.preprocessing.image image_generator = keras.preprocessing.image. ImageDataGenerator( rescale=1./255, shear_range=0.0,

    width_shift_range=0.1, height_shift_range=0.1, rotation_range=10, fill_mode="wrap", vertical_flip=True, horizontal_flip=True )
  4. # add 30% noise during training drop1 = keras.layers.SpatialDropout2D(0.3) (base_model.output)

    conv_filter = keras.layers.convolutional.Conv2D( 4, (1,1), activation="relu", use_bias=True, kernel_regularizer = keras.regularizers.l2(0.001) )(drop1) conv_filter input 7 x 7 x 4 base_model 224 x 224 x 3 7 x 7 x 2048
  5. # add 30% noise during training drop2 = keras.layers.SpatialDropout2D(0.3) (conv_filter)

    patch = keras.layers.convolutional.Conv2D( 2, (3, 3), name="patch", activation="softmax", use_bias=True, padding="same", kernel_regularizer= keras.regularizers.l2(0.001) )(drop2) patch_model = keras.models.Model( inputs=base_model.input, outputs=patch ) conv_filter patch input patch_model 7 x 7 x 2 7 x 7 x 4 224 x 224 x 3 7 x 7 x 2048
  6. pool = keras.layers.GlobalAveragePooling2D() (patch) logits = keras.layers.Activation("softmax") (pool) classifier =

    keras.models.Model( inputs=base_model.input, outputs=logits ) conv_filter patch pool logits input classifier ho g” / “no t ” 7 x 7 x 2 7 x 7 x 4 224 x 224 x 3 7 x 7 x 2048 2 2
  7. for layer in base_model.layers: layer.trainable = False classifier.compile( optimizer="rmsprop", loss="categorical_crossentropy",

    metrics=["accuracy"] ) classifier.fit_generator( train_generator, class_weight={0: .75, 1: .25}, epochs=10 ) base_model conv_filter patch pool logits input classifier train_generator
  8. Epoch 1/10 32/32 [==============================] - 148s 5s/step - loss: 0.3157

    - acc: 0.5051 Epoch 2/10 32/32 [==============================] - 121s 4s/step - loss: 0.3017 - acc: 0.5051 Epoch 3/10 32/32 [==============================] - 122s 4s/step - loss: 0.2961 - acc: 0.5010 Epoch 4/10 32/32 [==============================] - 121s 4s/step - loss: 0.2791 - acc: 0.5862 Epoch 5/10 32/32 [==============================] - 122s 4s/step - loss: 0.2681 - acc: 0.6380 Epoch 6/10 32/32 [==============================] - 123s 4s/step - loss: 0.2615 - acc: 0.6876 Epoch 7/10 32/32 [==============================] - 121s 4s/step - loss: 0.2547 - acc: 0.6790 Epoch 8/10 32/32 [==============================] - 122s 4s/step - loss: 0.2522 - acc: 0.7052 Epoch 9/10 32/32 [==============================] - 123s 4s/step - loss: 0.2522 - acc: 0.7045 Epoch 10/10 32/32 [==============================] - 145s 5s/step - loss: 0.2486 - acc: 0.7164 CPU times: user 1h 4min 20s, sys: 2min 35s, total: 1h 6min 56s Wall time: 21min 8s
  9. 60% hot dog 40% not hot dog + 40% -

    40% t a b e f o n s a c
  10. loop: async function() { this.offscreen.drawImage(this.video, 0, 0, 640, 480) var

    imageData = this.offscreen .getImageData(0, 0, 640, 480) var pixeldata = tf.fromPixels(imageData) var response = await tf.tidy(() => this.model.predict(preprocess(pixeldata)) ) responseData = await postprocess( response, pixeldata, 640, 480 ).data() for (var i = 0; i < responseData.length; i+=1) { imageData.data[i] = responseData[i] } this.onscreen.putImageData(imageData, 0, 0) this.continue() } of c model.predict() p e r s po p s co on r <vi >
  11. function preprocess(pixeldata) { // resize image for the model var

    contents = tf.image .resizeBilinear(pixeldata, [224, 224], false) // convert to float and make a one-image batch contents = contents .toFloat() .div(tf.scalar(255)) .expandDims(0) return contents } 224p 224p 640p 480p
  12. function postprocess(tensor, pixeldata, width, height) { var hotdog = 1

    var heatmap = tf.split(tensor.squeeze(), 2, 2)[hotdog] heatmap = tf.image.resizeBilinear(heatmap, [height, width], false) pixeldata = pixeldata.toFloat() var grayscale = pixeldata.mean(2).expandDims(2) grayscale = tf.onesLike(heatmap).sub(heatmap) .mul(grayscale).squeeze() .mul(tf.scalar(0.3)) grayscaleStacked = tf.stack([grayscale, grayscale, grayscale]).transpose([1,2,0]) composite = pixeldata.mul(heatmap).add(grayscaleStacked) var rgb = tf.split(composite, 3, 2) var alpha = tf.onesLike(rgb[0]).mul(tf.scalar(255)) rgb.push(alpha) var composite = tf.stack(rgb, 2) return composite.toInt() }
  13. var hotdog = 1 var heatmap = tf.split( tensor.squeeze(), 2,

    2 )[hotdog] heatmap = tf.image.resizeBilinear( heatmap, [640, 480], false ) 640p 480p 7 7 2 no t ho g
  14. pixeldata = pixeldata.toFloat() var grayscale = pixeldata.mean(2).expandDims(2) grayscale = tf.onesLike(heatmap).sub(heatmap)

    .mul(grayscale).squeeze() .mul(tf.scalar(0.3)) grayscaleStacked = tf.stack( [grayscale, grayscale, grayscale]) .transpose([1,2,0]) composite = pixeldata .mul(heatmap) .add(grayscaleStacked) var rgb = tf.split(composite, 3, 2) var alpha = tf.onesLike(rgb[0]) .mul(tf.scalar(255)) rgb.push(alpha) var composite = tf.stack(rgb, 2) return composite.toInt() pi d a he p 1-he p g a s co s e