Копия блокнота "2-top" Colaboratory



Yüklə 265,83 Kb.
Pdf görüntüsü
tarix21.12.2022
ölçüsü265,83 Kb.
#121573
2-topshiriq



30.11.2022, 12:52
Копия блокнота "2-top" - Colaboratory
https://colab.research.google.com/drive/1aTXtxrgxmTEGb7-DlPaU0LYNe2M-NrGD#scrollTo=Rmp5bsiqXi57&printMode=true
1/4
from tensorflow.keras.datasets import mnist #MNIST to'plamini chaqirish
from tensorflow.keras.datasets import cifar10 #CIFAR10 bazasini chaqirish
from tensorflow.keras.datasets import cifar100 #CIFAR100 to'plamini chaqirish
from tensorflow.keras.models import Sequential #Model
#Konvolution to'rlar uchun qavatlar
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout, BatchNormalization
from tensorflow.keras.preprocessing.image import ImageDataGenerator # rasmlar bilan ishlash uchun
from tensorflow.keras.optimizers import Adam, Adadelta # optimizatorlar
from tensorflow.keras import utils #to_categorical uchun ishlatamiz
from tensorflow.keras.preprocessing import image #Rasmlarni chizish uchun
from google.colab import files #O'zimizning faylni zagruzka qilish uchun
import numpy as np #Massivlar bilan ishlash uchun kutubxona
import matplotlib.pyplot as plt #Grafiklarni chizish uchun
from PIL import Image #Rasmlarni chizish uchun
import random #Taxminiy sonlarni generatsiya qilish uchun
import math # Yaxlitlash uchun ishlatamiz
import os #Fayllar bilan ishlash uchun
# google driveni ulaymiz
from google.colab import drive
%matplotlib inline
drive.mount('/content/drive')
Mounted at /content/drive
(x_train10, y_train10), (x_test10, y_test10) = cifar10.load_data()
Downloading data from 
https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170498071/170498071 [==============================] - 2s 0us/step
classes = ['samalyot', 'avtomobil', 'qush', 'mushuk', 'kiyik', 'kuchuk', 'qurbaqa', 'ot', 'kema', 'yukmoshin']
fig, axs = plt.subplots(1, 10, figsize=(25, 3)) 
for i in range(10): 
label_indexes = np.where(y_train10==i)[0] 
index = random.choice(label_indexes) 
img = x_train10[index] 
axs[i].imshow(Image.fromarray(img)) 
plt.show() 
y_train10 = utils.to_categorical(y_train10, 10)
y_test10 = utils.to_categorical(y_test10, 10)
print(x_train10.shape)
print(x_test10.shape)
print(y_train10.shape)
print(y_test10.shape)
#print(y_train10[0])
(50000, 32, 32, 3)
(10000, 32, 32, 3)
(50000, 10)
(10000, 10)
batch_size = 128 
#Ketma-ket modelini yasaymiz
model = Sequential()
#Paketli normalizatsiya qavati
model.add(BatchNormalization(input_shape=(32, 32, 3)))
#Birinchi konvolyutsion qavat
model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
#Ikkinchi konvolyutsion qavat
model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
#MaxPooling qavati
model.add(MaxPooling2D(pool_size=(2, 2)))


30.11.2022, 12:52
Копия блокнота "2-top" - Colaboratory
https://colab.research.google.com/drive/1aTXtxrgxmTEGb7-DlPaU0LYNe2M-NrGD#scrollTo=Rmp5bsiqXi57&printMode=true
2/4
#Dropout qavati (25 foiz)
model.add(Dropout(0.25))
#Paketli normalizatsiya qavati
model.add(BatchNormalization())
#Uchinchi konvolyutsion qavat
model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
#To'rtinchi konvolyutsion qavat
model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
#Ikkinchi Maxpooling2d qavati
model.add(MaxPooling2D(pool_size=(2, 2)))
#Dropout qavati
model.add(Dropout(0.25))
#Paketli normalizatsiya qavati
model.add(BatchNormalization())
#Beshinchi konvolyutsion qavat
model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))
#Oltinchi konvolyutsion qavat
model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))
#Uchinchi maxpooling qavati
model.add(MaxPooling2D(pool_size=(2, 2)))
#Dropout qavati
model.add(Dropout(0.25))
#Слой преобразования данных из 2D представления в плоское
model.add(Flatten())
#Полносвязный слой для классификации
model.add(Dense(512, activation='relu'))
#Dropout qavati
model.add(Dropout(0.25))
#Chiquvchi to'liq ulangan qavat
model.add(Dense(10, activation='softmax'))
#Tarmoqni kompilyatsiya qilamiz
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
#Tarmoqni cifar10 to'plami bilan o'qitamiz
history = model.fit(x_train10, 
y_train10, 
batch_size=batch_size, 
epochs=2, #Aslida 20
validation_data=(x_test10, y_test10),
verbose=1)
#O'qitish aniqligi grafigini chizamiz
plt.plot(history.history['accuracy'], 
label='Oqitish toplamidagi aniqlik miqdori')
plt.plot(history.history['val_accuracy'], 
label='Tekshitish toplamidagi aniqlik')
plt.xlabel('Epoxa')
plt.ylabel('Togri javoblar')
plt.legend()
plt.show() #Grafikni ekranga chiqaramiz
model.summary() #Tarmoq xisobotini chiqaramiz


30.11.2022, 12:52
Копия блокнота "2-top" - Colaboratory
https://colab.research.google.com/drive/1aTXtxrgxmTEGb7-DlPaU0LYNe2M-NrGD#scrollTo=Rmp5bsiqXi57&printMode=true
3/4
Epoch 1/2
391/391 [==============================] - 422s 1s/step - loss: 1.4605 - accurac
Epoch 2/2
391/391 [==============================] - 420s 1s/step - loss: 1.0265 - accurac
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
batch_normalization (BatchN (None, 32, 32, 3) 12
ormalization)
conv2d (Conv2D) (None, 32, 32, 32) 896
conv2d_1 (Conv2D) (None, 32, 32, 32) 9248
max_pooling2d (MaxPooling2D (None, 16, 16, 32) 0
)
dropout (Dropout) (None, 16, 16, 32) 0
batch_normalization_1 (Batc (None, 16, 16, 32) 128
hNormalization)
conv2d_2 (Conv2D) (None, 16, 16, 64) 18496
conv2d_3 (Conv2D) (None, 16, 16, 64) 36928
max_pooling2d_1 (MaxPooling (None, 8, 8, 64) 0
2D)
dropout_1 (Dropout) (None, 8, 8, 64) 0
batch_normalization_2 (Batc (None, 8, 8, 64) 256
hNormalization)
conv2d_4 (Conv2D) (None, 8, 8, 128) 73856
conv2d_5 (Conv2D) (None, 8, 8, 128) 147584
max_pooling2d_2 (MaxPooling (None, 4, 4, 128) 0
2D)
prediction = model.predict(x_test10)
313/313 [==============================] - 21s 65ms/step
n = random.randint(0,9999)
plt.imshow(Image.fromarray(x_test10[n]).convert('RGBA'))
plt.show()
print("Tarmoq javobi: ", prediction[n])
print("Topilgan javob: ", np.argmax(prediction[n]))
print("Togri javob: ", np.argmax(y_test10[n]))
print("Topilgan obraz: ", classes[np.argmax(prediction[n])])
print("Togri obraz: ", classes[np.argmax(y_test10[n])])
print("\n\nTarmoq javobi:\n", *[f"{i:.17f}\n" for i in prediction[n]])


30.11.2022, 12:52
Копия блокнота "2-top" - Colaboratory
https://colab.research.google.com/drive/1aTXtxrgxmTEGb7-DlPaU0LYNe2M-NrGD#scrollTo=Rmp5bsiqXi57&printMode=true
4/4

0 сек.
выполнено в 12:51
Платные продукты Colab 

Отменить подписку
Tarmoq javobi: [3.2728181e-05 1.3959901e-06 2.1257644e-04 2.2202508e-01 2.7878740
7.7719694e-01 5.6252160e-05 1.9288750e-04 2.1194933e-06 1.1534447e-06]
Topilgan javob: 5
Togri javob: 5
Topilgan obraz: kuchuk
Togri obraz: kuchuk
Tarmoq javobi:
0.00003272818139521
0.00000139599012527
0.00021257644402795
0.22202508151531219
0.00027878739638254
0.77719694375991821
0.00005625215999316
0.00019288749899715
0.00000211949327422
0.00000115344471396

Yüklə 265,83 Kb.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©muhaz.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin