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
| # _sleepData.json' | |
| def build_sleep_data(file_name): | |
| """ | |
| Take in ...sleepData.json file and output pandas | |
| Input: | |
| file_name | str | |
| _sleepData file | |
| Output | |
| sleep_pd | pdf | |
| pandas dataframe of sleep data |
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
| def load_image(img_path, show=False): | |
| img = image.load_img(img_path, target_size=(150, 150)) | |
| img_tensor = image.img_to_array(img) # (height, width, channels) | |
| img_tensor = np.expand_dims(img_tensor, axis=0) # (1, height, width, channels), add a dimension because the model expects this shape: (batch_size, height, width, channels) | |
| img_tensor /= 255. # imshow expects values in the range [0, 1] | |
| if show: | |
| plt.imshow(img_tensor[0]) | |
| plt.axis('off') |
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
| model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy']) | |
| # Adam optimizer | |
| # loss function will be categorical cross entropy | |
| # evaluation metric will be accuracy | |
| step_size_train=train_generator.n//train_generator.batch_size | |
| model.fit_generator(generator=train_generator, | |
| steps_per_epoch=step_size_train, | |
| epochs=10) |
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
| train_datagen=ImageDataGenerator(preprocessing_function=preprocess_input) #included in our dependencies | |
| train_generator=train_datagen.flow_from_directory('C:/Users/Ferhat/Python Code/Workshop/Tensoorflow transfer learning/downloads', | |
| target_size=(224,224), | |
| color_mode='rgb', | |
| batch_size=32, | |
| class_mode='categorical', | |
| shuffle=True) |
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
| for layer in model.layers: | |
| layer.trainable=False | |
| # or if we want to set the first 20 layers of the network to be non-trainable | |
| for layer in model.layers[:20]: | |
| layer.trainable=False | |
| for layer in model.layers[20:]: | |
| layer.trainable=True |
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
| for i,layer in enumerate(model.layers): | |
| print(i,layer.name) |
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
| model=Model(inputs=base_model.input,outputs=preds) | |
| #specify the inputs | |
| #specify the outputs | |
| #now a model has been created based on our architecture |
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_model=MobileNet(weights='imagenet',include_top=False) #imports the mobilenet model and discards the last 1000 neuron layer. | |
| x=base_model.output | |
| x=GlobalAveragePooling2D()(x) | |
| x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results. | |
| x=Dense(1024,activation='relu')(x) #dense layer 2 | |
| x=Dense(512,activation='relu')(x) #dense layer 3 | |
| preds=Dense(2,activation='softmax')(x) #final layer with softmax activation |
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
| !pip install google_images_download | |
| from google_images_download import google_images_download | |
| response = google_images_download.googleimagesdownload() | |
| arguments = {"keywords":"blue tit","limit":100,"print_urls":False,"format":"jpg", "size":">400*300"} | |
| paths = response.download(arguments) | |
| arguments = {"keywords":"crow","limit":100,"print_urls":False, "format":"jpg", "size":">400*300"} | |
| paths = response.download(arguments) |
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
| preprocessed_image = prepare_image('blue_tit.jpg') | |
| predictions = mobile.predict(preprocessed_image) | |
| results = imagenet_utils.decode_predictions(predictions) | |
| results |
NewerOlder