This file contains hidden or 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
| base_df = spark.read.text(raw_data_files) | |
| base_df.printSchema() |
This file contains hidden or 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 glob | |
| raw_data_files = glob.glob('*.gz') | |
| raw_data_files |
This file contains hidden or 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
| m = re.finditer(r'.*?(spark).*?', "I'm searching for a spark in PySpark", re.I) | |
| for match in m: | |
| print(match, match.start(), match.end()) |
This file contains hidden or 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
| # configure spark variables | |
| from pyspark.context import SparkContext | |
| from pyspark.sql.context import SQLContext | |
| from pyspark.sql.session import SparkSession | |
| sc = SparkContext() | |
| sqlContext = SQLContext(sc) | |
| spark = SparkSession(sc) | |
| # load up other dependencies |
This file contains hidden or 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 model_evaluation_utils as meu | |
| import pandas as pd | |
| basic_cnn_metrics = meu.get_metrics(true_labels=test_labels, predicted_labels=basic_cnn_pred_labels) | |
| vgg_frz_metrics = meu.get_metrics(true_labels=test_labels, predicted_labels=vgg_frz_pred_labels) | |
| vgg_ft_metrics = meu.get_metrics(true_labels=test_labels, predicted_labels=vgg_ft_pred_labels) | |
| pd.DataFrame([basic_cnn_metrics, vgg_frz_metrics, vgg_ft_metrics], | |
| index=['Basic CNN', 'VGG-19 Frozen', 'VGG-19 Fine-tuned']) |
This file contains hidden or 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
| # Load Saved Deep Learning Models | |
| basic_cnn = tf.keras.models.load_model('./basic_cnn.h5') | |
| vgg_frz = tf.keras.models.load_model('./vgg_frozen.h5') | |
| vgg_ft = tf.keras.models.load_model('./vgg_finetuned.h5') | |
| # Make Predictions on Test Data | |
| basic_cnn_preds = basic_cnn.predict(test_imgs_scaled, batch_size=512) | |
| vgg_frz_preds = vgg_frz.predict(test_imgs_scaled, batch_size=512) | |
| vgg_ft_preds = vgg_ft.predict(test_imgs_scaled, batch_size=512) |
This file contains hidden or 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
| test_imgs_scaled = test_data / 255. | |
| test_imgs_scaled.shape, test_labels.shape | |
| # Output | |
| ((8268, 125, 125, 3), (8268,)) |
This file contains hidden or 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
| tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1) | |
| reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, | |
| patience=2, min_lr=0.000001) | |
| callbacks = [reduce_lr, tensorboard_callback] | |
| train_steps_per_epoch = train_generator.n // train_generator.batch_size | |
| val_steps_per_epoch = val_generator.n // val_generator.batch_size | |
| history = model.fit_generator(train_generator, steps_per_epoch=train_steps_per_epoch, epochs=EPOCHS, | |
| validation_data=val_generator, validation_steps=val_steps_per_epoch, | |
| verbose=1) |
This file contains hidden or 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
| vgg = tf.keras.applications.vgg19.VGG19(include_top=False, weights='imagenet', | |
| input_shape=INPUT_SHAPE) | |
| # Freeze the layers | |
| vgg.trainable = True | |
| set_trainable = False | |
| for layer in vgg.layers: | |
| if layer.name in ['block5_conv1', 'block4_conv1']: | |
| set_trainable = True | |
| if set_trainable: |
This file contains hidden or 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
| img_id = 0 | |
| sample_generator = train_datagen.flow(train_data[img_id:img_id+1], train_labels[img_id:img_id+1], | |
| batch_size=1) | |
| sample = [next(sample_generator) for i in range(0,5)] | |
| fig, ax = plt.subplots(1,5, figsize=(16, 6)) | |
| print('Labels:', [item[1][0] for item in sample]) | |
| l = [ax[i].imshow(sample[i][0][0]) for i in range(0,5)] |