Example #1
0
    def test_model_pipe_mnist_urls(self):

        (x_train, y_train), (x_test, y_test) = mnist.load_data()

        x_train = x_train.reshape(60000, 28, 28, 1)
        x_test = x_test.reshape(10000, 28, 28, 1)
        x_train = x_train.astype('float32')
        x_test = x_test.astype('float32')
        x_train /= 255
        x_test /= 255

        y_train = to_categorical(y_train, 10)
        y_test = to_categorical(y_test, 10)

        model = Sequential()

        model.add(Flatten(input_shape=(28, 28, 1)))
        model.add(Dense(128, activation='relu'))
        model.add(Dense(128, activation='relu'))

        model.add(Dense(10, activation='softmax'))

        no_epochs = 3
        batch_size = 32

        model.compile(loss='categorical_crossentropy', optimizer='adam')

        model.fit(x_train,
                  y_train,
                  validation_data=(x_test, y_test),
                  epochs=no_epochs,
                  batch_size=batch_size)

        pipe = model_util.ModelPipe()

        pipe.add(lambda url: urllib.urlopen(url).read(),
                 num_workers=2,
                 timeout=10)
        pipe.add(lambda img: Image.open(BytesIO(img)))
        pipe.add(model_util.resize_to_model_input(model))
        pipe.add(lambda x: 1 - x)
        pipe.add(model, num_workers=1, batch_size=32, batcher=np.vstack)
        pipe.add(lambda x: np.argmax(x, axis=1))

        url5 = 'http://blog.otoro.net/assets/20160401/png/mnist_output_10.png'
        url2 = 'http://joshmontague.com/images/mnist-2.png'
        urlb = 'http://joshmontague.com/images/mnist-3.png'

        expected_output = {url5: 5, url2: 2}
        output = pipe({url5: url5, url2: url2, urlb: urlb})
        output = {k: v for k, v in output.iteritems() if v}

        self.assertEquals(output, expected_output)
Example #2
0
                         write_images=True)

model.fit(x_train,
          y_train,
          validation_data=(x_test, y_test),
          epochs=no_epochs,
          callbacks=[checkpointer, tbcallback],
          batch_size=batch_size)

class_labels = [
    'tshirt/top', 'trouser', 'pullover', 'dress', 'coat', 'sandal', 'shirt',
    'sneaker', 'bag', 'ankle boot'
]

pipe = model_util.ModelPipe()
pipe.add(lambda url: urllib.urlopen(url).read(), num_workers=2)
pipe.add(lambda img: Image.open(BytesIO(img)))
pipe.add(model_util.resize_to_model_input(model))
pipe.add(lambda x: 1 - x)
pipe.add(model, num_workers=1, batch_size=32, batcher=np.vstack)
pipe.add(lambda x: np.argmax(x, axis=1))
pipe.add(lambda x: [class_labels[int(x)]])

url_pants = 'https://asda.scene7.com/is/image/Asda/5054622127954_A'
url_boot = 'https://images-na.ssl-images-amazon.com/' + \
           'images/I/714sb6gwMpL._UL1500_.jpg'

url_broken = 'https://asda.scene7.com/is/image/Asda/5054622127954_B'
output = pipe({'pants': url_pants, 'boot': url_boot, 'broken': url_broken})
print output