Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +57 -0
- date-fruit-hurma-cnn.ipynb +1 -0
- model92.h5 +3 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
|
| 6 |
+
# Hurma çeşitleri
|
| 7 |
+
hurma_cesidi = ['Galaxy', 'Rutab', 'Sugaey', 'Medjool', 'Nabtat Ali',
|
| 8 |
+
'Ajwa', 'Sokari', 'Shaishe', 'Meneifi']
|
| 9 |
+
|
| 10 |
+
# Modeli yükleme
|
| 11 |
+
model = load_model('model92.h5')
|
| 12 |
+
model.summary()
|
| 13 |
+
|
| 14 |
+
def process_img(img):
|
| 15 |
+
img = img.resize((170, 170), Image.LANCZOS) # 170x170 piksel boyutuna dönüştürme
|
| 16 |
+
img = np.array(img) / 255.0 # Normalize etme
|
| 17 |
+
img = np.expand_dims(img, axis=0) # Resme boyut ekleme
|
| 18 |
+
return img
|
| 19 |
+
|
| 20 |
+
st.title("Hurma Çeşidi Sınıflandırması :date:")
|
| 21 |
+
st.write(
|
| 22 |
+
"Bir hurma resmi seçin ve modelimiz, bu resmin hangi hurma çeşidinden olduğunu tahmin etsin. 🖼️📊\n"
|
| 23 |
+
"Upload an image and the model will predict which date variety your image shows."
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Stil ayarları
|
| 27 |
+
st.markdown("""
|
| 28 |
+
<style>
|
| 29 |
+
.reportview-container {
|
| 30 |
+
background: #F0F2F6;
|
| 31 |
+
}
|
| 32 |
+
.sidebar .sidebar-content {
|
| 33 |
+
background: #E0E0E0;
|
| 34 |
+
}
|
| 35 |
+
.css-18e3th9 {
|
| 36 |
+
font-size: 1.25em;
|
| 37 |
+
color: #333;
|
| 38 |
+
}
|
| 39 |
+
</style>
|
| 40 |
+
""", unsafe_allow_html=True)
|
| 41 |
+
|
| 42 |
+
file = st.file_uploader("Resim Yükle & Bir resim seçiniz", type=['png', 'jpg', 'jpeg'])
|
| 43 |
+
|
| 44 |
+
if file is not None:
|
| 45 |
+
img = Image.open(file)
|
| 46 |
+
st.image(img, caption="Yüklenen Resim", use_column_width=True)
|
| 47 |
+
|
| 48 |
+
result = process_img(img)
|
| 49 |
+
prediction = model.predict(result)
|
| 50 |
+
prediction_class = np.argmax(prediction) # En yüksek tahmin edilen sınıf
|
| 51 |
+
|
| 52 |
+
# Sınıf isimleri
|
| 53 |
+
result_text = hurma_cesidi[prediction_class]
|
| 54 |
+
|
| 55 |
+
st.write(f"**Sonuç:** {result_text}")
|
| 56 |
+
else:
|
| 57 |
+
st.write("Lütfen bir resim yükleyin.")
|
date-fruit-hurma-cnn.ipynb
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":1432643,"sourceType":"datasetVersion","datasetId":839239}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n for filename in filenames:\n pass\n #print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-31T14:20:17.977390Z","iopub.execute_input":"2024-07-31T14:20:17.977786Z","iopub.status.idle":"2024-07-31T14:20:17.990422Z","shell.execute_reply.started":"2024-07-31T14:20:17.977757Z","shell.execute_reply":"2024-07-31T14:20:17.989258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"text-align: center;\">\n <h1>Görsel Açıdan Hurma</h1>\n\n <img src=\"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgusCi9yUQpg6PZo2aOSoR3DwuTieJZ8Iyar5ils9PZYPqxvaaW29c4_9d83xcC2nrRDCwhvOg-CQH-xl-JCgZAUr4NythtePPt61FQG3RLrRdWaKk68aEs2HZsop6BWJwp8UXc67zV-cAQNKiz6ztNe0O0lVuV3U58d0VnNP77NbXK2j7f5oMyG1hKDZL3/s1040/Medjool%20Dates%20500grams.jpeg\" alt=\"Hurma\" width=\"700\" height=\"500\">\n</div>\n","metadata":{}},{"cell_type":"markdown","source":"# Hurma CNN : Bu projemizde Hurmaların görsellerine bakara Convolitional Noural Network kullanarak onları sınıflandıracağız ","metadata":{}},{"cell_type":"markdown","source":"## İlk önce kaggle de bulunan bu hurma datalarını okumamız gerekiyor , bunun için bazı Python Kütüphanelerini kullanmamız gerekiyor ","metadata":{}},{"cell_type":"code","source":"# Data işlemlerini DataFrame oluşturma ve okumak için pandas kütüphanesi\nimport pandas as pd \n\n# Sistem uyarı ve hatalarını görmezden gelmek için\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# Rsim okumak için \nimport cv2 \n\n# bilgisayardaki dosyalrarı okumak için \nimport os # operating system ","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:04.988378Z","iopub.execute_input":"2024-07-31T07:50:04.989235Z","iopub.status.idle":"2024-07-31T07:50:07.639942Z","shell.execute_reply.started":"2024-07-31T07:50:04.989201Z","shell.execute_reply":"2024-07-31T07:50:07.639044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls '/kaggle/input/date-fruit-image-dataset-in-controlled-environment'\n","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:07.641965Z","iopub.execute_input":"2024-07-31T07:50:07.642510Z","iopub.status.idle":"2024-07-31T07:50:08.753502Z","shell.execute_reply.started":"2024-07-31T07:50:07.642474Z","shell.execute_reply":"2024-07-31T07:50:08.752453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Burada gördüğünüz üzere yokardaki ilk kodun yolunu kullanarak altındaki değer klasörleri görüyoruz\n#### Bu klasörler bizim Hurmaların Çeşitlerini gösteriyor , bunları resimleri guruplandırmak için kullanıcağız \n#### Amacımız Train ve Test ederken oluşturduğumuz Modelin Doğruluğunu hesaplamak olacak","metadata":{}},{"cell_type":"markdown","source":"# Datayı Oluşturma","metadata":{}},{"cell_type":"code","source":"path='/kaggle/input/date-fruit-image-dataset-in-controlled-environment'\nos.listdir(path) # Gördüğünüz gibi Bizim dosyalarımı bu şekilde \n# bu kullanım doğrudur # Galaxy den başlıyor çünkü ","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:08.755077Z","iopub.execute_input":"2024-07-31T07:50:08.755409Z","iopub.status.idle":"2024-07-31T07:50:08.764549Z","shell.execute_reply.started":"2024-07-31T07:50:08.755381Z","shell.execute_reply":"2024-07-31T07:50:08.763335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"örnek:\n/kaggle/input/date-fruit-image-dataset-in-controlled-environment/Galaxy/Galaxy Date (161).JPG","metadata":{}},{"cell_type":"code","source":"# dosya yolumuz\npath='/kaggle/input/date-fruit-image-dataset-in-controlled-environment'\n# çeşitlerimiz\nhurma_cesidi=['Galaxy','Rutab', 'Sugaey','Medjool','Nabtat Ali',\n 'Ajwa','Sokari','Shaishe','Meneifi']\n\n\nlabel_of_file=[]\nimg_list=[]\n\nfor kind in hurma_cesidi:\n kind_path = os.path.join(path, kind)\n \n for img in os.listdir(kind_path):\n \n img_list.append(os.path.join(kind_path, img))\n label_of_file.append(kind)\n ","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:08.766408Z","iopub.execute_input":"2024-07-31T07:50:08.766669Z","iopub.status.idle":"2024-07-31T07:50:09.160635Z","shell.execute_reply.started":"2024-07-31T07:50:08.766647Z","shell.execute_reply":"2024-07-31T07:50:09.159814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Verilen Değerleri Dataya Dönüştürme \n\ndf=pd.DataFrame({'img':img_list , 'label':label_of_file})\ndf.sample(7)","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:09.161978Z","iopub.execute_input":"2024-07-31T07:50:09.162651Z","iopub.status.idle":"2024-07-31T07:50:09.204386Z","shell.execute_reply.started":"2024-07-31T07:50:09.162615Z","shell.execute_reply":"2024-07-31T07:50:09.203319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Verimizin Rastgele 5 tane görselin bakalım ","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfor index, row in df.sample(7).iterrows():\n # Resmi yükle\n img = cv2.imread(row['img'])\n \n # OpenCV resmi RGB formatına dönüştür\n img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n \n # Resmi matplotlib ile göster\n plt.figure(figsize=(6, 6))\n plt.imshow(img_rgb)\n plt.title(row['label'])\n plt.axis('off')\n plt.show()\n \n # Her resim için kısa bir bekleme süresi ekleyin\n #plt.pause(1) # 1 saniye bekle","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:10.591257Z","iopub.execute_input":"2024-07-31T07:50:10.592068Z","iopub.status.idle":"2024-07-31T07:50:20.960017Z","shell.execute_reply.started":"2024-07-31T07:50:10.592038Z","shell.execute_reply":"2024-07-31T07:50:20.958961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info() # data hakkında bilgi ","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:20.961913Z","iopub.execute_input":"2024-07-31T07:50:20.962219Z","iopub.status.idle":"2024-07-31T07:50:20.988465Z","shell.execute_reply.started":"2024-07-31T07:50:20.962193Z","shell.execute_reply":"2024-07-31T07:50:20.987403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Hurmaların Dağılımına bakalım (meraktan)","metadata":{}},{"cell_type":"code","source":"#pip install plotly","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:20.989871Z","iopub.execute_input":"2024-07-31T07:50:20.990207Z","iopub.status.idle":"2024-07-31T07:50:21.000326Z","shell.execute_reply.started":"2024-07-31T07:50:20.990181Z","shell.execute_reply":"2024-07-31T07:50:20.999178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport plotly.express as px\nsayi = df['label'].value_counts() #hurma türe göre sayisi \n\n# Plotly ile hurma dağılımını görselleştirme\nfig=px.bar(x=sayi.index, #6 indexi var\n y=sayi.values, # sayisi \n color=sayi.index, # rekleri yine sayisina göre\n labels={'x': 'Çeşitler','y': 'Toplam Sayı'}, #label yazdırma\n title='Hurma Çeşitleri Grafiği', #başlık \n template='plotly_dark') # arkapılan \n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:21.002704Z","iopub.execute_input":"2024-07-31T07:50:21.003111Z","iopub.status.idle":"2024-07-31T07:50:22.815402Z","shell.execute_reply.started":"2024-07-31T07:50:21.003057Z","shell.execute_reply":"2024-07-31T07:50:22.814272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sayi = df['label'].value_counts()\nfig = px.pie(names=sayi.index,\n values=sayi.values,\n title='Hurma Dağılımı',\n labels={'names': 'Hurma',\n 'values': 'Toplam Sayı'},\n template='plotly_white')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:22.816787Z","iopub.execute_input":"2024-07-31T07:50:22.817103Z","iopub.status.idle":"2024-07-31T07:50:22.898002Z","shell.execute_reply.started":"2024-07-31T07:50:22.817077Z","shell.execute_reply":"2024-07-31T07:50:22.896956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Datanın Label Değrlerini Encode Etme ","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nencoder = LabelEncoder()\n\n# Etiketleri sayısal değerlere dönüştür\ndf['encode_label'] = encoder.fit_transform(df['label'])","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:22.899305Z","iopub.execute_input":"2024-07-31T07:50:22.899654Z","iopub.status.idle":"2024-07-31T07:50:24.928078Z","shell.execute_reply.started":"2024-07-31T07:50:22.899625Z","shell.execute_reply":"2024-07-31T07:50:24.927068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.sample(frac=1).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:24.929219Z","iopub.execute_input":"2024-07-31T07:50:24.929557Z","iopub.status.idle":"2024-07-31T07:50:24.935301Z","shell.execute_reply.started":"2024-07-31T07:50:24.929528Z","shell.execute_reply":"2024-07-31T07:50:24.934286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample(3)","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:24.936711Z","iopub.execute_input":"2024-07-31T07:50:24.937181Z","iopub.status.idle":"2024-07-31T07:50:24.952695Z","shell.execute_reply.started":"2024-07-31T07:50:24.937151Z","shell.execute_reply":"2024-07-31T07:50:24.951473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Normalizasyon","metadata":{}},{"cell_type":"code","source":"import numpy as np\nx=[]\nfor img in df['img']:\n try:\n img = cv2.imread(img)\n img = cv2.resize(img, (200, 200))# 448 pixle boyutunda\n img = img / 255.0 # normalize etmee \n x.append(img) # img \n\n except:\n print(f\"Error loading image: {img}\")","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:50:24.953961Z","iopub.execute_input":"2024-07-31T07:50:24.954232Z","iopub.status.idle":"2024-07-31T07:52:52.579889Z","shell.execute_reply.started":"2024-07-31T07:50:24.954208Z","shell.execute_reply":"2024-07-31T07:52:52.578852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Oluşturma \n### datamızın x,y değerlerini belirleyeceğiz sonra ise train ve test olarak ayıracağız\n","metadata":{}},{"cell_type":"code","source":"x=np.array(x)\ny=df['encode_label']","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:52:52.585641Z","iopub.execute_input":"2024-07-31T07:52:52.586241Z","iopub.status.idle":"2024-07-31T07:52:53.108500Z","shell.execute_reply.started":"2024-07-31T07:52:52.586213Z","shell.execute_reply":"2024-07-31T07:52:53.107407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:52:53.109797Z","iopub.execute_input":"2024-07-31T07:52:53.110238Z","iopub.status.idle":"2024-07-31T07:52:53.688007Z","shell.execute_reply.started":"2024-07-31T07:52:53.110198Z","shell.execute_reply":"2024-07-31T07:52:53.687110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape,x_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:52:53.689264Z","iopub.execute_input":"2024-07-31T07:52:53.689587Z","iopub.status.idle":"2024-07-31T07:52:53.697141Z","shell.execute_reply.started":"2024-07-31T07:52:53.689561Z","shell.execute_reply":"2024-07-31T07:52:53.696083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n# CNN Convolutional Netural Networks","metadata":{}},{"cell_type":"markdown","source":"## Bizim Yapacağmız işlemde bir nevi bu olacak \n\n\n","metadata":{}},{"cell_type":"markdown","source":"### konu hakkında daha fazla bilgi için :\n* https://medium.com/@kocelifk/evri%C5%9Fimli-sinir-a%C4%9Flar%C4%B1na-convolutional-neural-networks-cnn-giri%C5%9F-7b4b4d5d07b3\n* https://medium.com/@kubra.ozcan_98680/convolutional-neural-networks-cnn-nedir-a3a9d5318835","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:53:19.083343Z","iopub.execute_input":"2024-07-31T07:53:19.084192Z","iopub.status.idle":"2024-07-31T07:53:19.089346Z","shell.execute_reply.started":"2024-07-31T07:53:19.084155Z","shell.execute_reply":"2024-07-31T07:53:19.088355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model oluşturma\nmodel = models.Sequential()\n\n# Evrişimsel katmanlar(Conv) ve havuzlama(pooling) katmanları\n\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(200, 200, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.25)) # overfiting yapasın diye bazı değerleri atıyoruz dropout ile\n\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.25))\n\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.25))\n\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.25))\n\nmodel.add(layers.Conv2D(256, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.25))\n\n# Düzleştirme ve tam bağlı katmanlar\nmodel.add(layers.Flatten()) # düzleştirme\n\nmodel.add(layers.Dense(512, activation='relu'))\nmodel.add(layers.Dropout(0.5)) # Yüksek dropout oranı, tam bağlı katmanlarda aşırı öğrenmeyi azaltabilir\nmodel.add(layers.Dense(9, activation='softmax')) # Çıkış katmanı: 9 sınıf için # çeşit sayımıza göre \n\n# Modeli derleme\nmodel.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:53:20.576293Z","iopub.execute_input":"2024-07-31T07:53:20.577042Z","iopub.status.idle":"2024-07-31T07:53:21.900614Z","shell.execute_reply.started":"2024-07-31T07:53:20.577007Z","shell.execute_reply":"2024-07-31T07:53:21.899714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model özetini yazdır\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:53:23.665203Z","iopub.execute_input":"2024-07-31T07:53:23.665648Z","iopub.status.idle":"2024-07-31T07:53:23.710824Z","shell.execute_reply.started":"2024-07-31T07:53:23.665614Z","shell.execute_reply":"2024-07-31T07:53:23.709554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=45, batch_size=150)","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:58:00.022801Z","iopub.execute_input":"2024-07-31T07:58:00.023185Z","iopub.status.idle":"2024-07-31T07:59:23.535149Z","shell.execute_reply.started":"2024-07-31T07:58:00.023153Z","shell.execute_reply":"2024-07-31T07:59:23.533941Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Saving Model\nmodel.save('hurma_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:59:32.500681Z","iopub.execute_input":"2024-07-31T07:59:32.501092Z","iopub.status.idle":"2024-07-31T07:59:32.615616Z","shell.execute_reply.started":"2024-07-31T07:59:32.501062Z","shell.execute_reply":"2024-07-31T07:59:32.614697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Eğitim ve Doğrulama Kaybı\nplt.plot(history.history['loss'], label='Eğitim Kaybı')\nplt.plot(history.history['val_loss'], label='Doğrulama Kaybı')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()\n\n# Eğitim ve Doğrulama Doğruluğu\nplt.plot(history.history['accuracy'], label='Eğitim Doğruluğu')\nplt.plot(history.history['val_accuracy'], label='Doğrulama Doğruluğu')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:59:34.776073Z","iopub.execute_input":"2024-07-31T07:59:34.776772Z","iopub.status.idle":"2024-07-31T07:59:35.199674Z","shell.execute_reply.started":"2024-07-31T07:59:34.776739Z","shell.execute_reply":"2024-07-31T07:59:35.198591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss,acc=model.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:59:44.009898Z","iopub.execute_input":"2024-07-31T07:59:44.010334Z","iopub.status.idle":"2024-07-31T07:59:44.845483Z","shell.execute_reply.started":"2024-07-31T07:59:44.010270Z","shell.execute_reply":"2024-07-31T07:59:44.844520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc","metadata":{"execution":{"iopub.status.busy":"2024-07-31T07:59:56.277950Z","iopub.execute_input":"2024-07-31T07:59:56.278349Z","iopub.status.idle":"2024-07-31T07:59:56.284779Z","shell.execute_reply.started":"2024-07-31T07:59:56.278319Z","shell.execute_reply":"2024-07-31T07:59:56.283656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict=model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-31T08:00:03.037062Z","iopub.execute_input":"2024-07-31T08:00:03.037788Z","iopub.status.idle":"2024-07-31T08:00:03.848412Z","shell.execute_reply.started":"2024-07-31T08:00:03.037755Z","shell.execute_reply":"2024-07-31T08:00:03.847404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.metrics import f1_score,confusion_matrix,classification_report\nconfusion_matrix(y_test,np.argmax(predict,axis=1))\n\nsns.heatmap(confusion_matrix(y_test,np.argmax(predict,axis=1)),annot=True)\nplt.title('Confusion Matrix')","metadata":{"execution":{"iopub.status.busy":"2024-07-31T08:00:05.331586Z","iopub.execute_input":"2024-07-31T08:00:05.332397Z","iopub.status.idle":"2024-07-31T08:00:06.174645Z","shell.execute_reply.started":"2024-07-31T08:00:05.332362Z","shell.execute_reply":"2024-07-31T08:00:06.173681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test,np.argmax(predict,axis=1)))","metadata":{"execution":{"iopub.status.busy":"2024-07-31T08:00:14.058770Z","iopub.execute_input":"2024-07-31T08:00:14.059166Z","iopub.status.idle":"2024-07-31T08:00:14.078332Z","shell.execute_reply.started":"2024-07-31T08:00:14.059136Z","shell.execute_reply":"2024-07-31T08:00:14.077294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1_score(y_test, np.argmax(predict,axis=1), average='weighted')","metadata":{"execution":{"iopub.status.busy":"2024-07-31T08:00:17.345511Z","iopub.execute_input":"2024-07-31T08:00:17.345894Z","iopub.status.idle":"2024-07-31T08:00:17.357388Z","shell.execute_reply.started":"2024-07-31T08:00:17.345866Z","shell.execute_reply":"2024-07-31T08:00:17.356341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dışardan alınan Göresle ile Resmi Tahmin etme \n","metadata":{}},{"cell_type":"code","source":"import requests\nfrom PIL import Image\nfrom io import BytesIO\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.preprocessing.image import img_to_array\n","metadata":{"execution":{"iopub.status.busy":"2024-07-31T08:05:41.775848Z","iopub.execute_input":"2024-07-31T08:05:41.776898Z","iopub.status.idle":"2024-07-31T08:05:41.782260Z","shell.execute_reply.started":"2024-07-31T08:05:41.776861Z","shell.execute_reply":"2024-07-31T08:05:41.781148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Geçerli bir görsel URL'si\n#img_url = 'https://upload.wikimedia.org/wikipedia/commons/7/7e/Medjool-Date.jpg'\nimg_url='https://www.hurmashop.com/image//2021/mebrum_j_2021_001.jpg'\n\n# Medjool\n\n# Görseli URL'den indirin\nresponse = requests.get(img_url)\nimg = Image.open(BytesIO(response.content))\n\n# Görseli gösterme\nplt.imshow(img)\nplt.axis('off') # Eksenleri kapat\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-31T08:09:46.150210Z","iopub.execute_input":"2024-07-31T08:09:46.150907Z","iopub.status.idle":"2024-07-31T08:09:46.947217Z","shell.execute_reply.started":"2024-07-31T08:09:46.150875Z","shell.execute_reply":"2024-07-31T08:09:46.946110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Görseli yeniden boyutlandırma ve normalizasyon\ndef normalize_et(img):\n \n img = img.resize((200, 200)) # Modelin beklediği boyut\n img_array = img_to_array(img)\n img_array = img_array / 255.0 # Normalizasyon\n img_array = np.expand_dims(img_array, axis=0) # Batch boyutu ekleyin\n return img_array\n\nimg_array=normalize_et(img)\n# Model ile tahmin yapma\ntahmin = model.predict(img_array)\n\ntahim_sinifi = np.argmax(tahmin, axis=1) # kaç tane dğer var ise ona göre en yakını bulmaya çalışıyor \n#print(tahim_sinifi)\n\n# Tahmin edilen sınıfı yazdırma\nprint(f\"Tahmin edilen sınıf: {tahim_sinifi[0]}\\nYani : {df[df['encode_label'] == tahim_sinifi[0]]['label'].iloc[0]}\")","metadata":{"execution":{"iopub.status.busy":"2024-07-31T08:10:59.653512Z","iopub.execute_input":"2024-07-31T08:10:59.654365Z","iopub.status.idle":"2024-07-31T08:10:59.732084Z","shell.execute_reply.started":"2024-07-31T08:10:59.654332Z","shell.execute_reply":"2024-07-31T08:10:59.730942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sonuç. CNN ile Modelimizi oluşturduk ve Gerekli olan değerleri verdik \n# bizim için 9 tane hurma çeşitinden internette aldığmız çeşiti tahmin etmeyi başardı \n# bir sonraki adım bunu hugieface ye app olarak yüklemek olacak onun linki\n> link:","metadata":{}},{"cell_type":"markdown","source":" ## bir derin öğrenme modelini eğitim ve değerlendirme aşamalarında aşırı uyumu (overfitting) önlemek ve modelin genelleme yeteneğini artırmak için gerekli adımları içeren bir örnek sunar. ","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, GlobalAveragePooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom sklearn.model_selection import train_test_split\n\n# Verileri eğitim ve doğrulama setlerine ayırma\nX_train, X_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=42)\n\n# Veri Augmentasyonu\ndatagen = ImageDataGenerator(\n rotation_range=30,\n width_shift_range=0.3,\n height_shift_range=0.3,\n shear_range=0.3,\n zoom_range=0.3,\n horizontal_flip=True,\n fill_mode='nearest'\n)\n\ndatagen.fit(X_train)\n\n# Transfer Öğrenme Modeli (VGG16)\nbase_model = VGG16(weights='imagenet', include_top=False, input_shape=(200, 200, 3))\nmodel = Sequential([\n base_model,\n GlobalAveragePooling2D(),\n Dense(256, activation='relu'),\n Dropout(0.5),\n Dense(9, activation='softmax')\n])\n\n# Modeli derleme\noptimizer = Adam(learning_rate=0.0001)\nmodel.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Erken Durdurma\nearly_stopping = EarlyStopping(monitor='val_loss', patience=5)\n\n# Modeli eğitim\nhistory = model.fit(\n datagen.flow(X_train, y_train, batch_size=32),\n validation_data=(X_val, y_val),\n epochs=50,\n callbacks=[early_stopping]\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-31T08:13:41.235692Z","iopub.status.idle":"2024-07-31T08:13:41.236182Z","shell.execute_reply.started":"2024-07-31T08:13:41.235932Z","shell.execute_reply":"2024-07-31T08:13:41.235952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"başarı oranı 92","metadata":{}},{"cell_type":"code","source":"# Saving Model\nmodel.save('my_hurma_new_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-07-30T12:54:37.521492Z","iopub.execute_input":"2024-07-30T12:54:37.521976Z","iopub.status.idle":"2024-07-30T12:54:37.975847Z","shell.execute_reply.started":"2024-07-30T12:54:37.521929Z","shell.execute_reply":"2024-07-30T12:54:37.974766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}
|
model92.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4a4c32d3c7052f242a32b9899fa879cc23bddd18f09952dd25dab7d86e4ed5cd
|
| 3 |
+
size 20724352
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
streamlit
|