Exemplo n.º 1
0
def create_dataset():
    print('criando dataset')
    data = ImageClassifierDataLoader.from_folder('./images_dataset')
    train_data, rest_data = data.split(0.8)
    validation_data, test_data = rest_data.split(0.5)
    model = image_classifier.create(train_data,
                                    validation_data=validation_data,
                                    epochs=10)
    model.summary()
    model.export(export_dir='model/', export_format=ExportFormat.LABEL)
    model.export(export_dir='model/')
Exemplo n.º 2
0
def run(data_dir, export_dir, spec='efficientnet_lite0', **kwargs):
  """Runs demo."""
  spec = model_spec.get(spec)
  data = image_classifier.DataLoader.from_folder(data_dir)
  train_data, rest_data = data.split(0.8)
  validation_data, test_data = rest_data.split(0.5)

  model = image_classifier.create(
      train_data, model_spec=spec, validation_data=validation_data, **kwargs)

  _, acc = model.evaluate(test_data)
  print('Test accuracy: %f' % acc)

  # Exports to TFLite and SavedModel, with label file.
  export_format = [
      ExportFormat.TFLITE,
      ExportFormat.SAVED_MODEL,
  ]
  model.export(export_dir, export_format=export_format)
Exemplo n.º 3
0
"""generates tfjs model from files"""
import os

import numpy as np
import tensorflow as tf

assert tf.__version__.startswith("2")

from tflite_model_maker import model_spec
from tflite_model_maker import image_classifier
from tflite_model_maker.config import ExportFormat
from tflite_model_maker.config import QuantizationConfig
from tflite_model_maker.image_classifier import DataLoader

import matplotlib.pyplot as plt

IMAGE_PATH = "/home/uni/storage/p-block/"
MODEL_PATH = "/home/uni/repo/p-block/generated-model"
data = DataLoader.from_folder(IMAGE_PATH)
train_data, test_data = data.split(0.9)

model = image_classifier.create(train_data)
# loss, accuracy = model.evaluate(test_data)
# 0.9273504018783569
# 0.3174212872982025
# model.export('/home/uni/repo/p-block/generated-model')
model.export(MODEL_PATH, export_format=ExportFormat.TFJS)
Exemplo n.º 4
0
Here, we use EfficientNet Lite1 as a base model for image classification and build custom tflite model

More info, click here:
<br>
https://blog.tensorflow.org/2020/03/higher-accuracy-on-vision-models-with-efficientnet-lite.html
"""

efficientnet_model = model_spec.get("efficientnet_lite1")

"""### **8). Training and Creating**
Here, we begin train the entire datasets and also create custom model based on pre-trained model
"""

model = image_classifier.create(train_data,
                                epochs=10,
                                validation_data=validation_data,
                                use_augmentation=True,
                                shuffle=True,
                                model_spec=efficientnet_model)

"""### **9). Display Model Summary**"""

model.summary()

"""### **10). Display Model Training-Validation Loss and Accuracy**"""

# Commented out IPython magic to ensure Python compatibility.
# %matplotlib inline

#Loss graph
plt.figure(figsize=(8, 6))
plt.plot(model.history.history["loss"])
Exemplo n.º 5
0
Tensorflow Lite Model Maker ile Uygulama için Hazır Edilmesi (Mobil Kullanım için)

pip install tflite-model-maker

import tensorflow as tf
assert tf.__version__.startswith('2')

from tflite_model_maker import configs
from tflite_model_maker import ExportFormat
from tflite_model_maker import image_classifier
from tflite_model_maker import ImageClassifierDataLoader
from tflite_model_maker import model_spec

import matplotlib.pyplot as plt

# Eğitim, Değerlendirme ve Test Verilerinin Yüklenmesi 
training_data = ImageClassifierDataLoader.from_folder('/content/drive/MyDrive/onayli_onaysiz_512x512/training/')
validation_data = ImageClassifierDataLoader.from_folder('/content/drive/MyDrive/onayli_onaysiz_512x512/validation/')
test_data = ImageClassifierDataLoader.from_folder('/content/drive/MyDrive/onayli_onaysiz_512x512/test/')
print(len(validation_data))
print(len(test_data))

# Modelin Eğitilmesi
model = image_classifier.create(training_data, validation_data=validation_data, epochs=10)

# Modelin Test Verileriye Onaylanması
loss, accuracy = model.evaluate(test_data)

# Tensorflow Lite Modelin Yüklenmesi ve bu Dosya Android Studio tarafından kullanılabilecektir
model.export(export_dir='.')
Exemplo n.º 6
0
    seed=12,
    image_size=(imgHeight, imgWidth),
    batch_size=batchSize)

# Save labels
classNames = trainDataset.class_names
print("Classes names:%s" % classNames)
np.savetxt(labelsPath, classNames, '%s', delimiter='\n')
print("Control class names:")
for s in classNames:
    print(s, end=', ')
print()
print("Control labels save to path: %s" % labelsPath)

# Load data
data = ImageClassifierDataLoader.from_folder(path)
trainData, testData = data.split(0.62)
trainData = data

# Create model
model = image_classifier.create(trainDataset)
model.summary()

# Evaluate the model
loss, acc = model.evaluate(testData)
print("Model accuracy: %.4f" % acc)
print("Model loss: %.4f" % loss)

# Export model
model.export(modelPath)
Exemplo n.º 7
0
    'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
    untar=True)

data = ImageClassifierDataLoader.from_folder(image_path)
train_data, rest_data = data.split(0.8)
validation_data, test_data = rest_data.split(0.5)
plt.figure(figsize=(10, 10))
for i, (image, label) in enumerate(data.dataset.take(25)):
    plt.subplot(5, 5, i + 1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(image.numpy(), cmap=plt.cm.gray)
    plt.xlabel(data.index_to_label[label.numpy()])
plt.show()
model = image_classifier.create(train_data, validation_data=validation_data)
model.summary()
loss, accuracy = model.evaluate(test_data)


def get_label_color(val1, val2):
    if val1 == val2:
        return 'black'
    else:
        return 'red'


# Then plot 100 test images and their predicted labels.
# If a prediction result is different from the label provided label in "test"
# dataset, we will highlight it in red color.
plt.figure(figsize=(20, 20))
Exemplo n.º 8
0
# for i, (image, label) in enumerate(data.gen_dataset().unbatch().take(25)):
#   plt.subplot(5,5,i+1)
#   plt.xticks([])
#   plt.yticks([])
#   plt.grid(False)
#   plt.imshow(image.numpy(), cmap=plt.cm.gray)
#   plt.xlabel(data.index_to_label[label.numpy()])
# plt.show()

# model = image_classifier.create(train_data, validation_data=validation_data)
# model.summary()

train_data, test_data = data.split(0.8)

#SINGLE MODEL
model = image_classifier.create(train_data, dropout_rate=0.1)

#Evaluating model
loss, accuracy = model.evaluate(test_data)

# #Export TFLite file
model.export(export_dir='.')

# #Export LABEL file
model.export(export_dir='.', export_format=ExportFormat.LABEL)

#Post Training optimization
config = configs.QuantizationConfig.create_full_integer_quantization(
    representative_data=test_data, is_integer_only=True)

#Exporting optimized model
Exemplo n.º 9
0
import tensorflow as tf
assert tf.__version__.startswith('2')

from tflite_model_maker import model_spec
from tflite_model_maker import image_classifier
from tflite_model_maker.config import ExportFormat
from tflite_model_maker.config import QuantizationConfig
from tflite_model_maker.image_classifier import DataLoader

import matplotlib.pyplot as plt

#%% Config
# Based on: https://codelabs.developers.google.com/tflite-computer-vision-train-model
image_path = "D:\\GIT\\machine-learning-project\\main\\_data_224\\"
EPOCHS = 1

data = DataLoader.from_folder(image_path)
train_data, test_data = data.split(0.9)

#%% Training
model = image_classifier.create(train_data, epochs=EPOCHS)

#%% evaluation
loss, accuracy = model.evaluate(test_data)

#%% export
model.export(export_dir='.', export_format=ExportFormat.SAVED_MODEL)
model.export(export_dir='.', export_format=ExportFormat.LABEL)

# %%