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 tensorflow as tf | |
| from tensorflow.python import keras | |
| from tensorflow.python.keras.models import Sequential | |
| from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout | |
| from tensorflow.python.keras.layers import MaxPooling2D, BatchNormalization | |
| from keras.utils.vis_utils import model_to_dot | |
| from keras.utils import plot_model | |
| from tensorflow.python.keras.callbacks import ModelCheckpoint | |
| from tensorflow.keras.callbacks import EarlyStopping | |
| from sklearn.model_selection import train_test_split |
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
| np.random.seed(SEED) | |
| tf.random.set_random_seed(SEED) | |
| model = Sequential() | |
| model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(IMG_ROWS, IMG_COLS, 1))) | |
| model.add(Conv2D(32, (3, 3), activation='relu')) | |
| model.add(MaxPooling2D(pool_size=(2, 2))) | |
| model.add(Conv2D(64, (3, 3), activation='relu')) | |
| model.add(MaxPooling2D(pool_size=(2, 2))) | |
| model.add(Flatten()) | |
| model.add(Dense(128, activation='relu')) |
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
| feature_extractor_url = "https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/3" | |
| def feature_extractor(x): | |
| feature_extractor_module = hub.Module(feature_extractor_url) | |
| return feature_extractor_module(x) | |
| IMAGE_SIZE = hub.get_expected_image_size(hub.Module(feature_extractor_url)) | |
| print('Expected size : %s' % IMAGE_SIZE) | |
| # Expected size : [224, 224] |
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
| training_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255., | |
| rotation_range=30, | |
| zoom_range=0.3, | |
| width_shift_range=0.2, | |
| height_shift_range=0.2, | |
| fill_mode='nearest', | |
| horizontal_flip=True) | |
| validation_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.) | |
| testing_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.) | |
| training_generator = training_datagen.flow_from_directory('../data/Train/', |
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
| malaria_model = tf.keras.Sequential([features_extractor_layer, | |
| layers.Dense(training_generator.num_classes, | |
| activation='softmax') | |
| ]) | |
| malaria_model.summary() | |
| malaria_model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=0.001), | |
| loss='categorical_crossentropy', | |
| metrics=['acc']) | |
| """ |
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
| sess = K.get_session() | |
| init = tf.global_variables_initializer() | |
| sess.run(init) | |
| STEP_SIZE_TRAIN = training_generator.samples // training_generator.batch_size | |
| STEP_SIZE_VALID = validatation_generator.n // validatation_generator.batch_size | |
| training_stats = CollectStats() | |
| history = malaria_model.fit_generator(training_generator, | |
| epochs=50, | |
| steps_per_epoch=STEP_SIZE_TRAIN, | |
| validation_data=validatation_generator, |
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
| STEP_SIZE_TEST = testing_generator.n // testing_generator.batch_size | |
| testing_generator.reset() | |
| predictions = malaria_model.predict_generator(testing_generator, | |
| steps=STEP_SIZE_TEST, | |
| verbose=1) | |
| img_name = testing_generator.filenames | |
| results = pd.DataFrame({'img_name': img_name, | |
| 'prediction': predicted_class}) | |
| _this = results.img_name.str.split('\\', n=1, expand=True) | |
| results['reference'] = _this[0] |
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
| from sklearn.datasets import make_blobs | |
| import numpy as np | |
| SEED = 123 | |
| N_SAMPLES = 10000 | |
| N_FEATURES = 2 | |
| N_CENTERS = 5 | |
| np.random.seed(SEED) | |
| X, y = make_blobs(n_samples=N_SAMPLES, n_features=N_FEATURES, centers=N_CENTERS, |
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 numpy as np | |
| import matplotlib.pyplot as plt | |
| start_tm, end_tm = 0, 2 | |
| signal1 = 12 # frequency of the wave | |
| smpl_freq = 32 * signal1 # sampling frequency with oversampling factor=32 | |
| smpl_intv = 1 / smpl_freq # intervals time points are sampled | |
| tm = np.arange(start_tm, end_tm, smpl_intv) | |
| ampl1 = np.sin(2 * np.pi * signal1 * tm) # generate sine wave |
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
| fig, axes = plt.subplots(2, 1, figsize=(14, 6)) | |
| plt.subplots_adjust(hspace=.5) | |
| axes[0].set_title(f'Wave with a frequency of {signal1} Hz') | |
| axes[0].plot(tm, ampl1) | |
| axes[0].set_xlabel('Time') | |
| axes[0].set_ylabel('Amplitude') | |
| ft_ = np.fft.fft(ampl1) / len(ampl1) # Normalize amplitude and apply the FFT | |
| ft_ = ft_[range(int(len(ampl1)/2))] # Exclude sampling frequency | |
| tp_cnt = len(ampl1) | |
| val_ = np.arange(int(tp_cnt / 2)) |