Skip to content

Instantly share code, notes, and snippets.

View SmiffyKMc's full-sized avatar
🚀

Kieran McCarthy SmiffyKMc

🚀
View GitHub Profile
@SmiffyKMc
SmiffyKMc / mobilenet.py
Created June 24, 2022 17:54
MobileNet model
import tensorflow as tf
from tensorflow import keras
from keras import layers
conv_base = keras.applications.mobilenet_v2.MobileNetV2(
weights="imagenet",
include_top=False
)
conv_base.trainable = False
@SmiffyKMc
SmiffyKMc / multiclassifier.py
Last active June 24, 2022 16:46
Multiclassifier
conv_base = keras.applications.vgg16.VGG16(
weights="imagenet",
include_top=False
)
conv_base.trainable = False
inputs = keras.Input(shape=(256, 256, 3))
x = data_augmentation(inputs)
x = keras.applications.vgg16.preprocess_input(x)
x = conv_base(x)
name: tflite_image_classification
description: A new Flutter project.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.16.1 <3.0.0"
@SmiffyKMc
SmiffyKMc / home.dart
Created June 24, 2022 11:21
The home file
import 'package:flutter/material.dart';
import 'package:tflite_image_classification/tflite_model.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@SmiffyKMc
SmiffyKMc / tflite_model.dart
Last active June 24, 2022 11:41
The model widget.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:tflite/tflite.dart';
class TfliteModel extends StatefulWidget {
const TfliteModel({Key? key}) : super(key: key);
@override
@SmiffyKMc
SmiffyKMc / convert_to_lite.py
Created June 17, 2022 20:30
Converting model to Tensorflow Lite
from tensorflow import keras
import pathlib
test_model = keras.models.load_model(f"{hotDogDir}hotdog_classifier_v4.keras")
converter = tensorflow.lite.TFLiteConverter.from_keras_model(test_model)
tflite_model = converter.convert()
tflite_models_dir = pathlib.Path(f"{hotDogDir}")
tflite_models_dir.mkdir(exist_ok=True, parents=True)
@SmiffyKMc
SmiffyKMc / test_prediction_func.py
Created June 17, 2022 19:44
Function to test predictions
from PIL import Image
import tensorflow as tf
from tensorflow import keras
from keras import layers
import os
import matplotlib.pyplot as plt
import numpy as np
def download_and_predict(url, filename):
test_model = keras.models.load_model(f"{hotDogDir}hotdog_classifier_v4.keras")
@SmiffyKMc
SmiffyKMc / freezed_conv.py
Created June 17, 2022 16:50
Freezed layers for our CNN
conv_base = keras.applications.vgg16.VGG16(
weights="imagenet",
include_top=False
)
conv_base.trainable = False
inputs = keras.Input(shape=(256, 256, 3))
x = data_augmentation(inputs)
x = keras.applications.vgg16.preprocess_input(x)
x = conv_base(x)
@SmiffyKMc
SmiffyKMc / model_v3.py
Created June 17, 2022 15:06
V3 of the model
inputs = keras.Input(shape=(8, 8, 512))
x = layers.Flatten()(inputs)
x = layers.Dense(256)(x)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(1, activation= keras.activations.sigmoid)(x)
model = keras.Model(inputs, outputs)
model.compile(optimizer=keras.optimizers.RMSprop(),
loss=keras.losses.BinaryCrossentropy(),
metrics=["accuracy"])
@SmiffyKMc
SmiffyKMc / get_features.py
Created June 17, 2022 15:01
Retrieving the features of the VGG16 model
import numpy as np
def get_features_and_labels(dataset):
all_features = []
all_labels = []
for images, labels in dataset:
preprocessed_images = keras.applications.vgg16.preprocess_input(images)
features = conv_base.predict(preprocessed_images)
all_features.append(features)
all_labels.append(labels)