def receive_data(childPipe): context = zmq.Context() sub_recv = context.socket(zmq.SUB) sub_recv.connect("tcp://" + IP + ":5557") sub_recv.setsockopt(zmq.SUBSCRIBE, b'') while True: b_data_array = sub_recv.recv_pyobj() childPipe.send(b_data_array) #print("send to pipe") continue global model model = MobileNet(weights=None, input_shape=(32, 32, 3), classes=10) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) def get_gradient_func(model): grads = K.gradients(model.total_loss, model.trainable_weights) inputs = model._feed_inputs + model._feed_targets + model._feed_sample_weights func = K.function(inputs, grads) return func def trainHandler(event, context): worker_ids = event["worker_ids"] # the allocate work id bat_size = event["batch_size"] max_fnum = event["fuction_number"] snumber = event["sub_data_size"]
# ImageNet weights, basic fully connected (dense) top elif option == 3: image_input = tf.keras.Input(shape=x_train.shape[1:], name='image_input') x = MobileNet(weights='imagenet', include_top=False)(image_input) x = tf.keras.layers.Flatten(name='flatten')(x) x = tf.keras.layers.Dense(4096, activation='relu', name='fc1')(x) x = tf.keras.layers.Dense(4096, activation='relu', name='fc2')(x) x = tf.keras.layers.Dense(num_classes, activation='softmax', name='predictions')(x) model = tf.keras.Model(inputs=image_input, outputs=x) model.summary() # Select loss, optimizer, metric model.compile(loss='categorical_crossentropy', optimizer=tf.train.AdamOptimizer(0.001), metrics=['accuracy']) # Train start = timer() model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test)) # callbacks=[TensorBoardColabCallback(tbc)] # Evaluate score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1])
nb_validation_samples = 50000 #%% # model setup def top5_acc(y_true, y_pred): return metrics.top_k_categorical_accuracy(y_true, y_pred, k=5) print('Building model...') t = time.time() #model = ResNet50(weights='../resnet50_weights_tf_dim_ordering_tf_kernels.h5') model = MobileNet(weights='../mobilenet_1_0_224_tf.h5') model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy', top5_acc]) t = time.time() - t #model.summary() print('model build time: %f s' % t) #%% img_path = '../test_images/wine.jpg' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x)