This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
# define API urls | |
route_url = "https://rt.data.gov.hk/v1.1/transport/citybus-nwfb/route/{company_code}" | |
route_stop_url = "https://rt.data.gov.hk/v1.1/transport/citybus-nwfb/route-stop/{company_code}/{route}/{direction}" | |
stop_url = "https://rt.data.gov.hk/v1.1/transport/citybus-nwfb/stop/{stop}" | |
# request header | |
headers = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
getContentUrl: (id) => `http://www.abc.com/${id}.html`, | |
getFileName: (id) => `content${id}.jpg` | |
} | |
/* | |
Usage | |
const conf = require('./export-string-literals'); | |
conf.getContentUrl('123'); => http://www.abc.com/123.html | |
conf.getFileName('456'); => content456.jpg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def extract_InceptionV3(tensor): | |
from keras.applications.inception_v3 import InceptionV3, preprocess_input | |
return InceptionV3(weights='imagenet', include_top=False).predict(preprocess_input(tensor)) | |
def InceptionV3_predict_breed(img_path): | |
# extract bottleneck features | |
bottleneck_feature = extract_InceptionV3(path_to_tensor(img_path)) | |
# obtain predicted vector | |
predicted_vector = InceptionV3_model.predict(bottleneck_feature) | |
# return dog breed that is predicted by the model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
InceptionV3_model.load_weights('saved_models/weights.best.InceptionV3.hdf5') | |
# get index of predicted dog breed for each image in test set | |
InceptionV3_predictions = [np.argmax(InceptionV3_model.predict(np.expand_dims(feature, axis=0))) for feature in test_InceptionV3] | |
# report test accuracy | |
test_accuracy = 100*np.sum(np.array(InceptionV3_predictions)==np.argmax(test_targets, axis=1))/len(InceptionV3_predictions) | |
print('Test accuracy: %.4f%%' % test_accuracy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
InceptionV3_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) | |
checkpointer = ModelCheckpoint(filepath='saved_models/weights.best.InceptionV3.hdf5', | |
verbose=1, save_best_only=True) | |
InceptionV3_model.fit(train_InceptionV3, train_targets, | |
validation_data=(valid_InceptionV3, valid_targets), | |
epochs=20, batch_size=20, callbacks=[checkpointer], verbose=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
InceptionV3_model = Sequential() | |
InceptionV3_model.add(GlobalAveragePooling2D(input_shape=train_InceptionV3.shape[1:])) | |
InceptionV3_model.add(Dense(133, activation='softmax')) | |
InceptionV3_model.summary() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bottleneck_features = np.load('bottleneck_features/DogInceptionV3Data.npz') | |
train_InceptionV3 = bottleneck_features['train'] | |
valid_InceptionV3 = bottleneck_features['valid'] | |
test_InceptionV3 = bottleneck_features['test'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model.load_weights('saved_models/weights.best.from_scratch.hdf5') | |
# get index of predicted dog breed for each image in test set | |
dog_breed_predictions = [np.argmax(model.predict(np.expand_dims(tensor, axis=0))) for tensor in test_tensors] | |
# report test accuracy | |
test_accuracy = 100*np.sum(np.array(dog_breed_predictions)==np.argmax(test_targets, axis=1))/len(dog_breed_predictions) | |
print('Test accuracy: %.4f%%' % test_accuracy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.callbacks import ModelCheckpoint | |
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) | |
### specify the number of epochs that you would like to use to train the model. | |
epochs = 5 | |
checkpointer = ModelCheckpoint(filepath='saved_models/weights.best.from_scratch.hdf5', | |
verbose=1, save_best_only=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D | |
from keras.layers import Dropout, Flatten, Dense | |
from keras.models import Sequential | |
model = Sequential() | |
### TODO: Define your architecture. | |
# image is 224×224x3 pixels | |
model.add(Conv2D(filters=16, kernel_size=(2,2), activation='relu', input_shape=(224,224,3))) | |
model.add(MaxPooling2D(pool_size=(2, 2), strides=None, padding='same', data_format=None)) |
NewerOlder