<jupyter_start><jupyter_text>2 - Image Classification with DNN- categories: [jupyter, dnn, fingerprint]  DATASETS:(a) Carbonic Anhydrase II (ChEMBL205), a protein lyase,  (b) Cyclin-dependent kinase 2 (CHEMBL301), a protein kinase,  (c) ether-a-go-go-related gene potassium channel 1 (HERG) (CHEMBL240), a voltage-gated ion channel,  (d) Dopamine D4 receptor (CHEMBL219), a monoamine GPCR,  (e) Coagulation factor X (CHEMBL244), a serine protease,  (f) Cannabinoid CB1 receptor (CHEMBL218), a lipid-like GPCR and  (g) Cytochrome P450 19A1 (CHEMBL1978), a cytochrome P450.  The activity classes were selected based on data availability and as representatives of therapeutically important target classes or as anti-targets.<jupyter_code>#hide
!nvidia-smi
#hide
import pandas as pd
import numpy as np
from pathlib import Path
#hide
%matplotlib inline
import matplotlib.pyplot as plt
import sys
import os
sys.path.append('/usr/local/lib/python3.7/site-packages/')
from rdkit import Chem
from rdkit.Chem import AllChem
#hide
dataset = 'CHEMBL205_cl'
#hide
path = Path('dataset/13321_2017_226_MOESM1_ESM/')
df = pd.read_csv(path/f'{dataset}.csv')
#hide
list(path.iterdir())<jupyter_output><empty_output><jupyter_text>Generate images<jupyter_code>DATA = path/'mol_images'
DATA.mkdir(exist_ok=True)
df.head()
#hide
#DElETE DIRECTORY
#import shutil
#import pathlib
#import os  # for checking results

#print(os.listdir())
# ["a_directory", "foo.py", ...]

#DATA = DATA/'directory'

#shutil.rmtree(DATA)
#print(os.listdir())
# ["foo.py", ...]<jupyter_output><empty_output><jupyter_text>Here we are creating images(.pngs) of the fingerprints from all of the molecules in the specific dataset by using functions from rdKit library.<jupyter_code># Iterate all rows in the dataframe and create images

IMAGES = DATA/dataset
if not IMAGES.is_dir():
    IMAGES.mkdir(exist_ok=True)
    for i, r in df.iterrows():
    
        cid = r.CID
        smile = r.SMILES
        mol = Chem.MolFromSmiles(smile)
        Chem.Draw.MolToFile(mol, IMAGES/f'{cid}.png', size = (224, 224), imageType='png')
images = list(IMAGES.glob('*.png'))<jupyter_output><empty_output><jupyter_text>Plotting a specified fingerprint image of a molecule, image was created above.<jupyter_code>#hide_input
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread(images[0])
imgplot = plt.imshow(img)
plt.show()<jupyter_output><empty_output><jupyter_text>Create train validation split<jupyter_code>#hide
from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>Splitting the dataset into a training and a validation set.<jupyter_code>x_train, x_valid = train_test_split(df.index, test_size=0.2, random_state=666, stratify=df['Activity'])
df.loc[x_train, 'is_valid']=False
df.loc[x_valid, 'is_valid']=True
#hide
df.is_valid.value_counts()<jupyter_output><empty_output><jupyter_text>Create dataloader<jupyter_code>#hide
from fastai.vision.all import *<jupyter_output><empty_output><jupyter_text>Matching the CID with the .png and creating an extra column for extracting data.<jupyter_code>df['Image'] = df['CID'] + '.png'
df.head()
images = df['CID'] + ".png"
images.head()
db = DataBlock(
    blocks = (ImageBlock(), CategoryBlock()), 
    get_x=ColReader('Image', pref=IMAGES),
    get_y=ColReader('Activity'), 
    splitter=ColSplitter('is_valid'),
    item_tfms=None, 
    batch_tfms=None
    )
dls = db.dataloaders(df, bs=64, shuffle_train=True)
dls.show_batch()<jupyter_output><empty_output><jupyter_text>Train CNN model<jupyter_code>#hide
from sklearn.metrics import auc,roc_auc_score,recall_score,precision_score,f1_score
from sklearn.metrics import matthews_corrcoef
from sklearn.metrics import accuracy_score<jupyter_output><empty_output><jupyter_text>Creating a function for training the CNN model<jupyter_code>def train_model(dls, arch=resnet18, epochs=5, freeze_epochs=5, wd=None):
    
    print(f'Training model on dataset: {dataset}')
    print(f'Architechture: {arch}')
    print(f'Untrained epochs: freeze_epochs={freeze_epochs}')
    print(f'Trained epochs: epochs={epochs}')
    
    learn = cnn_learner(dls, arch=resnet18, pretrained=True, 
                            wd=wd,
                            metrics=[accuracy, 
                            F1Score(), 
                            Precision(), 
                            Recall(), 
                            RocAucBinary(), 
                            MatthewsCorrCoef()])
    
    
    print(f'Finding learning rate...')
    lr_min, lr_steep = learn.lr_find(suggestions=True, show_plot=False)
    print(f'Training model with learning rate: {lr_min}')
    learn.fine_tune(epochs, lr_min, freeze_epochs=freeze_epochs)
    
    return learn<jupyter_output><empty_output><jupyter_text>Training the model on a pretrained 18 layer convolutional neural network (resnet18)<jupyter_code># Train model on resnet18
learn = train_model(dls)
#collapse_output
# Train on resnet34
learn = train_model(dls, arch=resnet34)
interp = ClassificationInterpretation.from_learner(learn)<jupyter_output><empty_output><jupyter_text>Plotting the confusion matrix for visualising the performance of our classification model.<jupyter_code>interp.plot_confusion_matrix()<jupyter_output><empty_output><jupyter_text>Plotting the molecules/fingerprints with highest loss in our imageset.<jupyter_code>interp.plot_top_losses(k=5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11102927637635794, 'fraction_numerical': 0.01601914932793224, 'mean_word_length': 4.9823788546255505, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '58574', 'license': 'Apache-2.0', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1852, 'n_words': 420, 'path': '_notebooks/2021-04-24-2.0-image_classification.ipynb', 'repo_name': 'Drug-Discovery/Drug-Discovery-blog', 'size': '5431'}