|
| 1 | +from __future__ import print_function |
| 2 | +import dotscience as ds |
| 3 | +import os |
| 4 | + |
| 5 | +# defaults to connecting to prod |
| 6 | +ds.connect( |
| 7 | + os.getenv("DOTSCIENCE_USERNAME"), |
| 8 | + os.getenv("DOTSCIENCE_APIKEY"), |
| 9 | + os.getenv("DOTSCIENCE_PROJECT_NAME"), |
| 10 | + os.getenv("DOTSCIENCE_HOSTNAME") |
| 11 | +) |
| 12 | + |
| 13 | +import tensorflow as tf |
| 14 | +from tensorflow.keras.datasets import mnist |
| 15 | +from tensorflow.keras.models import Sequential |
| 16 | +from tensorflow.keras.layers import Dense, Dropout, Flatten |
| 17 | +from tensorflow.keras.layers import Conv2D, MaxPooling2D |
| 18 | +from tensorflow.keras import backend as K |
| 19 | + |
| 20 | +tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR) |
| 21 | + |
| 22 | +batch_size = ds.parameter("batch_size", 128) |
| 23 | +num_classes = ds.parameter("num_classes", 10) |
| 24 | +epochs = ds.parameter("epochs", 1) |
| 25 | + |
| 26 | +# input image dimensions |
| 27 | +img_rows, img_cols = 28, 28 |
| 28 | +limit = ds.parameter("limit", 1000) |
| 29 | + |
| 30 | +# the data, split between train and test sets |
| 31 | +(x_train, y_train), (x_test, y_test) = mnist.load_data() |
| 32 | +x_train = x_train[:limit] |
| 33 | +y_train = y_train[:limit] |
| 34 | + |
| 35 | +if K.image_data_format() == 'channels_first': |
| 36 | + x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) |
| 37 | + x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) |
| 38 | + input_shape = (1, img_rows, img_cols) |
| 39 | +else: |
| 40 | + x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) |
| 41 | + x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) |
| 42 | + input_shape = (img_rows, img_cols, 1) |
| 43 | + |
| 44 | +x_train = x_train.astype('float32') |
| 45 | +x_test = x_test.astype('float32') |
| 46 | +x_train /= 255 |
| 47 | +x_test /= 255 |
| 48 | +print('x_train shape:', x_train.shape) |
| 49 | +print(x_train.shape[0], 'train samples') |
| 50 | +print(x_test.shape[0], 'test samples') |
| 51 | + |
| 52 | +# convert class vectors to binary class matrices |
| 53 | +y_train = tf.keras.utils.to_categorical(y_train, num_classes) |
| 54 | +y_test = tf.keras.utils.to_categorical(y_test, num_classes) |
| 55 | + |
| 56 | +model = Sequential() |
| 57 | +model.add(Conv2D(32, kernel_size=(3, 3), |
| 58 | + activation='relu', |
| 59 | + input_shape=input_shape)) |
| 60 | +model.add(Conv2D(64, (3, 3), activation='relu')) |
| 61 | +model.add(MaxPooling2D(pool_size=(2, 2))) |
| 62 | +model.add(Dropout(0.25)) |
| 63 | +model.add(Flatten()) |
| 64 | +model.add(Dense(128, activation='relu')) |
| 65 | +model.add(Dropout(0.5)) |
| 66 | +model.add(Dense(num_classes, activation='softmax')) |
| 67 | + |
| 68 | +model.compile(loss=tf.keras.losses.categorical_crossentropy, |
| 69 | + optimizer=tf.keras.optimizers.Adam(), |
| 70 | + metrics=['accuracy']) |
| 71 | + |
| 72 | +model.fit(x_train, y_train, |
| 73 | + batch_size=batch_size, |
| 74 | + epochs=epochs, |
| 75 | + verbose=1, |
| 76 | + validation_data=(x_test, y_test)) |
| 77 | +score = model.evaluate(x_test, y_test, verbose=0) |
| 78 | +print('Test loss:', ds.metric("loss", score[0])) |
| 79 | +print('Test accuracy:', ds.metric("accuracy", score[1])) |
| 80 | + |
| 81 | +#model.save("model.h5") |
| 82 | +#tf.keras.experimental.export_saved_model(model, ds.model(tf, 'model')) |
| 83 | + |
| 84 | + |
| 85 | +tf.keras.experimental.export_saved_model(model, ds.output('model')) |
| 86 | +ds.model(tf, "mnist", "model", classes="classes.json") |
| 87 | +ds.publish("trained mnist model", deploy=True) |
0 commit comments