| | |
| | """Untitled20.ipynb |
| | |
| | Automatically generated by Colaboratory. |
| | |
| | Original file is located at |
| | https://colab.research.google.com/drive/1O_tHcmidNGKAgxAiG7Su44auJSRFR1xA |
| | """ |
| |
|
| | import tensorflow as tf |
| | from tensorflow.keras.models import Sequential |
| | from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout |
| | from tensorflow.keras.preprocessing.image import ImageDataGenerator |
| |
|
| | image_size = (128, 128) |
| | batch_size = 32 |
| | train_datagen = ImageDataGenerator( |
| | rescale=1./255, |
| | shear_range=0.2, |
| | zoom_range=0.2, |
| | horizontal_flip=True |
| | ) |
| |
|
| | test_datagen = ImageDataGenerator(rescale=1./255) |
| |
|
| | train_generator = train_datagen.flow_from_directory( |
| | '/content/drive/MyDrive/training', |
| | target_size=image_size, |
| | batch_size=batch_size, |
| | class_mode='binary' |
| | ) |
| |
|
| | test_generator = test_datagen.flow_from_directory( |
| | '/content/drive/MyDrive/testing', |
| | target_size=image_size, |
| | batch_size=batch_size, |
| | class_mode='binary' |
| | ) |
| |
|
| | from tensorflow.keras.models import Sequential |
| |
|
| | model = Sequential() |
| | model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(image_size[0], image_size[1], 3))) |
| | model.add(MaxPooling2D(pool_size=(2, 2))) |
| | model.add(Conv2D(64, (3, 3), activation='relu')) |
| | model.add(MaxPooling2D(pool_size=(2, 2))) |
| | model.add(Conv2D(128, (3, 3), activation='relu')) |
| | model.add(MaxPooling2D(pool_size=(2, 2))) |
| | model.add(Flatten()) |
| | model.add(Dense(128, activation='relu')) |
| | model.add(Dropout(0.5)) |
| | model.add(Dense(1, activation='sigmoid')) |
| |
|
| | model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) |
| | model.fit(train_generator, epochs=10, validation_data=test_generator) |
| | evaluation = model.evaluate(test_generator) |
| | print(f"Test Accuracy: {evaluation[1] * 100:.2f}%") |
| |
|
| | predictions = model.predict(test_generator) |
| | predicted_labels = (predictions > 0.5).astype(int) |
| |
|
| | from sklearn.metrics import confusion_matrix, classification_report |
| | true_labels = test_generator.classes |
| | conf_matrix = confusion_matrix(true_labels, predicted_labels) |
| | print("Confusion Matrix:") |
| | print(conf_matrix) |
| | class_report = classification_report(true_labels, predicted_labels, target_names=['not_fractured', 'fractured']) |
| | print("Classification Report:") |
| | print(class_report) |
| |
|
| | import matplotlib.pyplot as plt |
| | import random |
| | test_images, true_labels = next(test_generator) |
| | predicted_labels = (model.predict(test_images) > 0.5).astype(int) |
| | plt.figure(figsize=(12, 8)) |
| | for i in range(10): |
| | plt.subplot(2, 5, i+1) |
| | plt.imshow(test_images[i]) |
| | plt.title(f"True: {true_labels[i]}, Predicted: {predicted_labels[i]}") |
| | plt.axis('off') |
| | plt.show() |
| |
|
| | import cv2 |
| |
|
| | image = cv2.imread('/content/drive/MyDrive/testing/fractured/1-rotated1-rotated1-rotated2.jpg') |
| |
|
| | plt.imshow(image) |
| |
|
| | image.shape |
| |
|
| | image = cv2.resize(image,(256,256)) |
| |
|
| | test_input = image.reshape((1,256,256,3)) |
| |
|
| | image.shape |
| |
|
| | plt.imshow(image) |
| |
|
| | test_input = image.reshape((1,256,256,3)) |
| |
|
| | !pip install keras |
| | import keras |
| | model = keras.Sequential([ |
| | keras.layers.Dense(128, activation="relu"), |
| | keras.layers.Dense(64, activation="relu"), |
| | keras.layers.Dense(10, activation="softmax") |
| | ]) |
| |
|
| | !ls -l model |
| |
|
| | !stat model |
| |
|
| | !file model |
| |
|
| | !pip show tensorflow |
| |
|
| | model.predict(test_input) |
| |
|
| |
|