{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-26T09:22:42.376046Z","iopub.execute_input":"2024-08-26T09:22:42.377038Z","iopub.status.idle":"2024-08-26T09:22:42.385603Z","shell.execute_reply.started":"2024-08-26T09:22:42.376988Z","shell.execute_reply":"2024-08-26T09:22:42.384420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import datasets, layers, models\nimport matplotlib.pyplot as plt\n\n(x_train, y_train), (x_test, y_test) = datasets.cifar10.load_data()\n\n# Rozmiar danych\nprint(\"Train data shape: \", x_train.shape)\nprint(\"Test data shape: \", x_test.shape)\n\n# Wyświetlenie kilku losowych obrazów\nfor i in range(9):\n    plt.subplot(330 + 1 + i)\n    plt.imshow(x_train[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-26T09:38:20.366818Z","iopub.execute_input":"2024-08-26T09:38:20.367327Z","iopub.status.idle":"2024-08-26T09:38:31.853505Z","shell.execute_reply.started":"2024-08-26T09:38:20.367281Z","shell.execute_reply":"2024-08-26T09:38:31.852010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Przekształcanie danych wejściowych (x_train, x_test)\nx_train = x_train.astype('float32') / 255.0\nx_test = x_test.astype('float32') / 255.0\n#Przekształcanie etykiet (y_train, y_test) na one-hot encoding\nfrom tensorflow.keras.utils import to_categorical\n\ny_train = to_categorical(y_train, 10)\ny_test = to_categorical(y_test, 10)\n\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-26T09:38:36.459486Z","iopub.execute_input":"2024-08-26T09:38:36.460222Z","iopub.status.idle":"2024-08-26T09:38:36.737509Z","shell.execute_reply.started":"2024-08-26T09:38:36.460173Z","shell.execute_reply":"2024-08-26T09:38:36.736348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Budowa modelu\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(10, activation='softmax')\n])\n\n# Kompilacja modelu\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Trenowanie modelu\nhistory = model.fit(x_train, y_train, epochs=20, batch_size=64, validation_data=(x_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2024-08-26T09:38:44.711686Z","iopub.execute_input":"2024-08-26T09:38:44.712545Z","iopub.status.idle":"2024-08-26T09:50:41.160689Z","shell.execute_reply.started":"2024-08-26T09:38:44.712493Z","shell.execute_reply":"2024-08-26T09:50:41.158999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='accuracy')\nplt.plot(history.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0, 1])\nplt.legend(loc='lower right')\nplt.show()\n\ntest_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)\nprint(f\"Test Accuracy: {test_acc}\")","metadata":{"execution":{"iopub.status.busy":"2024-08-26T09:51:02.689245Z","iopub.execute_input":"2024-08-26T09:51:02.689771Z","iopub.status.idle":"2024-08-26T09:51:05.686076Z","shell.execute_reply.started":"2024-08-26T09:51:02.689723Z","shell.execute_reply":"2024-08-26T09:51:05.684869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('cifar10_cnn_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-08-26T09:52:05.378199Z","iopub.execute_input":"2024-08-26T09:52:05.379546Z","iopub.status.idle":"2024-08-26T09:52:05.442548Z","shell.execute_reply.started":"2024-08-26T09:52:05.379486Z","shell.execute_reply":"2024-08-26T09:52:05.441161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nnew_model = tf.keras.models.load_model('cifar10_cnn_model.h5')\n\n# Pobierz przykładowe obrazy testowe (dla przykładu pierwsze 15 obrazów z zestawu testowego)\nsample_images = x_test[:15]\nsample_labels = y_test[:15]\n\n# Dokonaj przewidywania\npredictions = new_model.predict(sample_images)\n\n# Konwersja wyników do czytelnego formatu\npredicted_classes = np.argmax(predictions, axis=1)\nactual_classes = np.argmax(sample_labels, axis=1)\n\n# Wyświetl wyniki przewidywania\nclass_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']\n\nfor i, image in enumerate(sample_images):\n    plt.imshow(image)\n    plt.title(f\"Predicted: {class_names[int(predicted_classes[i])]}\\nActual: {class_names[int(actual_classes[i])]}\")\n    plt.axis('off') \n    plt.show()\n\n# Symulowana historia treningu\nhistory = {\n    'loss': np.random.randn(20).cumsum() + 10,\n    'val_loss': np.random.randn(20).cumsum() + 10,\n    'accuracy': np.linspace(0.5, 0.8, 20),\n    'val_accuracy': np.linspace(0.5, 0.7, 20)\n}\n\n# Wyświetlenie wykresów strat oraz dokładności\nplt.figure(figsize=(12, 4))\n\nplt.subplot(1, 2, 1)\nplt.plot(history['loss'], label='Strata na treningu')\nplt.plot(history['val_loss'], label='Strata na walidacji')\nplt.xlabel('Epoki')\nplt.ylabel('Strata')\nplt.legend(loc='upper right')\nplt.title('Strata w czasie treningu')\n\nplt.subplot(1, 2, 2)\nplt.plot(history['accuracy'], label='Dokładność na treningu')\nplt.plot(history['val_accuracy'], label='Dokładność na walidacji')\nplt.xlabel('Epoki')\nplt.ylabel('Dokładność')\nplt.legend(loc='lower right')\nplt.title('Dokładność w czasie treningu')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-26T10:08:48.115405Z","iopub.execute_input":"2024-08-26T10:08:48.115899Z","iopub.status.idle":"2024-08-26T10:08:50.364963Z","shell.execute_reply.started":"2024-08-26T10:08:48.115860Z","shell.execute_reply":"2024-08-26T10:08:50.363884Z"},"trusted":true},"execution_count":null,"outputs":[]}]}