<jupyter_start><jupyter_text>VISUALIZATION OF DEFECT AND CORRECT ONES<jupyter_code>image=cv2.imread('../input/real-life-industrial-dataset-of-casting-product/casting_data/casting_data/train/ok_front/cast_ok_0_1092.jpeg')
plt.title("OK IMAGE")
plt.imshow(image)
image=cv2.imread("../input/real-life-industrial-dataset-of-casting-product/casting_data/casting_data/train/def_front/cast_def_0_1007.jpeg")
plt.title("DEFECT IMAGE")
plt.imshow(image)


from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_gen=ImageDataGenerator(rescale=1.0/255.0)
                           
                           
validation_gen=ImageDataGenerator(rescale=1.0/255.0)
trainDatagen=train_gen.flow_from_directory(train_dir,
                                          target_size=(300,300),
                                           batch_size=100,
                                           class_mode='binary')
valDatagen=validation_gen.flow_from_directory(test_dir,
                                              target_size=(300,300),
                                           batch_size=64,
                                           class_mode='binary')

def create_model(input_shape):
   
    base_model = tf.keras.applications.Xception(input_shape = input_shape,
                       include_top = False)
                      

    # we do not have to train all of the layers
    for layer in base_model.layers:
        layer.trainable = False
        
    x = tf.keras.layers.Flatten()(base_model.output)
    x = tf.keras.layers.Dense(512, activation = 'relu')(x)
    x = tf.keras.layers.Dropout(0.4)(x)
    x = tf.keras.layers.Dense(1, activation = 'sigmoid')(x)
    
    return tf.keras.models.Model(base_model.input,x)
model = create_model((300,300,3))
model.summary()
model.compile(loss='binary_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])
class mycallbacks(tf.keras.callbacks.Callback):
    def on_epoch_end(self,epochs,logs={}):
        if(logs.get('accuracy')>=1.0):
            self.model.stop_training=True
callbacks=mycallbacks()
history=model.fit_generator(trainDatagen,validation_data=valDatagen,epochs=20,verbose=1,callbacks=[callbacks])
trainDatagen.class_indices
pred_probability = model.predict_generator(valDatagen)
valDatagen.classes
from sklearn.metrics import classification_report
predictions = pred_probability > 0.5
print(classification_report(valDatagen.classes,predictions))
def prediction(image):
    img=cv2.imread(image)
    img=img/255
    pred = model.predict(img.reshape(-1,300,300,3))
    if (pred<0.5):
        print("def_front")
        cv2.putText(img, "def_front", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
    else:
        print("ok_front")
        cv2.putText(img, "ok_front", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
    
    plt.imshow(img,cmap='gray')
    plt.axis('off')
    plt.show()
prediction('../input/real-life-industrial-dataset-of-casting-product/casting_data/casting_data/test/def_front/cast_def_0_1153.jpeg')<jupyter_output>def_front
{'fraction_non_alphanumeric': 0.12292142158461036, 'fraction_numerical': 0.035213563743071406, 'mean_word_length': 2.81592039800995, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '405378', 'license': 'MIT', 'n_tokens_mistral': 1077, 'n_tokens_neox': 993, 'n_words': 125, 'path': 'casting-product-defect.ipynb', 'repo_name': 'siddarth09/AI-automation', 'size': '3067'}