text_plain_output_1.png
from tensorflow import keras

from tensorflow.keras import layers

from tensorflow.keras.models import Sequential

import cv2

import numpy as np

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import tensorflow as tf

df = pd.read_csv('/kaggle/input/sports-classification/sports.csv')
test = df[df['data set'] == 'test']
df = df[df['data set'] == 'train']

df = df.sample(frac=1)

data_augmentation = tf.keras.Sequential([layers.RandomFlip('horizontal_and_vertical'), layers.RandomRotation(0.2)])

model = Sequential([keras.layers.InputLayer(input_shape=(224, 224, 3)), data_augmentation, keras.layers.Conv2D(64, 3, activation='relu'), keras.layers.MaxPooling2D(), keras.layers.Conv2D(32, 3, activation='relu'), keras.layers.MaxPooling2D(), keras.layers.Conv2D(16, 3, activation='relu'), keras.layers.MaxPooling2D(), keras.layers.Flatten(), keras.layers.Dense(200, activation='relu'), keras.layers.Dense(100, activation='sigmoid')])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

def get_pic(filepath):
    img = cv2.imread('/kaggle/input/sports-classification/' + filepath)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    return img

d = {}
u = list(df['labels'].unique())
for i, j in enumerate(u):
    d[j] = i

x_test = list(test['filepaths'].apply(get_pic))
y_test = list(test['labels'].apply(lambda x: d[x]))
y_test = np.array(y_test)
x_test = np.array(x_test) / 255

img_loc = []
y_train = []
for i in range(100):
    start = 0
    temp_df = df.sample(1000)
    img_loc = list(temp_df['filepaths'].apply(get_pic))
    y_train = list(temp_df['labels'].apply(lambda x: d[x]))
    y_train = np.array(y_train)
    img_loc = np.array(img_loc) / 255
    model.fit(img_loc, y_train, epochs=1, batch_size=64)