Skip to content

Instantly share code, notes, and snippets.

View ocinpp's full-sized avatar

ocinpp

  • Hong Kong
View GitHub Profile
@ocinpp
ocinpp / data-gov-hk-ctb-nwfb.py
Created March 26, 2023 14:12
Get route, route stops and stop info of NWFB and CTB from data.gov.hk
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 = {
@ocinpp
ocinpp / export-string-literals.js
Created April 22, 2019 19:13
Exporting String Literals
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
@ocinpp
ocinpp / dog_breed_inception_predict.py
Created February 17, 2019 18:14
Dog Breed - InceptionV3 Predict
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
@ocinpp
ocinpp / dog_breed_inception_load_and_test.py
Created February 17, 2019 18:06
Dog Breed - InceptionV3 Load and Test
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)
@ocinpp
ocinpp / dog_breed_inception_compile_and_train.py
Last active February 17, 2019 18:03
Dog Breed - InceptoinV3 Compile and Train
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)
@ocinpp
ocinpp / dog_breed_inception_sequential.py
Created February 17, 2019 18:00
Dog Breed - InceptionV3 Sequential
InceptionV3_model = Sequential()
InceptionV3_model.add(GlobalAveragePooling2D(input_shape=train_InceptionV3.shape[1:]))
InceptionV3_model.add(Dense(133, activation='softmax'))
InceptionV3_model.summary()
@ocinpp
ocinpp / dog_breed_inception_bottleneck.py
Created February 17, 2019 17:30
Dog Breed - InceptionV3 Bottleneck Features
bottleneck_features = np.load('bottleneck_features/DogInceptionV3Data.npz')
train_InceptionV3 = bottleneck_features['train']
valid_InceptionV3 = bottleneck_features['valid']
test_InceptionV3 = bottleneck_features['test']
@ocinpp
ocinpp / dog_breed_simple_cnn_load_test.py
Created February 17, 2019 16:21
Dog Breed - Simple CNN Load and Test
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)
@ocinpp
ocinpp / dog_breed_simple_cnn_train.py
Last active February 17, 2019 16:16
Dog Breed - Simple CNN Train
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)
@ocinpp
ocinpp / dog_breed_simple_cnn.py
Last active February 17, 2019 17:01
Dog Breed - Simple CNN
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))