DEV Community

KateMLady
KateMLady

Posted on • Edited on

If trying write AI

We tolk a lot about new trend writting code with help AI. If you look into it, it will become obviously: AI capable of replacing small parts of modern code in companies.
Today AI much more effective in areas: detecting objects, words bots and computer vision.

Image description

On picture not very hard neural network, which based on a series of convolutions and pulls. This particular design names UNet-Segmentation.

  • Some useful libraries will help to impact data for training network: numpy, pandas, matplotlib
df = pd.read_csv('data/train_masks.csv')

train_df = df[:4000]
val_df = df[4000:]

img_name, mask_rle = train_df.iloc[4]

img = cv2.imread('data/train/{}'.format(img_name))
mask = rle_decode(mask_rle)
Enter fullscreen mode Exit fullscreen mode
  • Next step to success coding AI: copying achitecture to Python (I usually use Google Colab/Jupyter Notebook). Might help: keras
conv_1_1 = Conv2D(32, (3, 3), padding='same')(inp) 
conv_1_1 = Activation('relu')(conv_1_1) 

conv_1_2 = Conv2D(32, (3, 3), padding='same')(conv_1_1)
conv_1_2 = Activation('relu')(conv_1_2)

pool_1 = MaxPooling2D(2)(conv_1_2)
Enter fullscreen mode Exit fullscreen mode
  • The last one: model training. Sometimes it takes a little time (for me ~ 7 minutes) for complete all areas
model.fit_generator(keras_generator(train_df, batch_size),
              steps_per_epoch=100, 
              epochs=100, 
              verbose=1, 
              callbacks=callbacks, 
              validation_data=keras_generator(val_df, batch_size),
              validation_steps=50,
              class_weight=None,
              max_queue_size=10,
              workers=1,
              use_multiprocessing=False,
              shuffle=True,
              initial_epoch=0)
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)