क्रॉपनेट: कसावा रोग का पता लगाना

TensorFlow.org पर देखें Google Colab में चलाएं गिटहब पर देखें नोटबुक डाउनलोड करें टीएफ हब मॉडल देखें

इस नोटबुक से पता चलता है कि कैसे CropNet उपयोग करने के लिए कसावा रोग वर्गीकारक TensorFlow हब से मॉडल। बैक्टीरियल तुषार, भूरे रंग लकीर रोग, हरी घुन, मोज़ेक रोग, स्वस्थ, या अज्ञात: 6 वर्गों में से एक में कसावा पत्तियों के मॉडल का वर्गीकरण छवियों।

यह कोलाब दर्शाता है कि कैसे:

  • लोड https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2 TensorFlow हब से मॉडल
  • लोड कसावा TensorFlow डेटासेट से डाटासेट (TFDS)
  • कसावा के पत्तों की छवियों को 4 अलग कसावा रोग श्रेणियों में या स्वस्थ या अज्ञात के रूप में वर्गीकृत करें।
  • कैसे मजबूत मॉडल जब डोमेन छवियों से बाहर करने के लिए लागू है पर वर्गीकारक और देखो की सटीकता का मूल्यांकन करें।

आयात और सेटअप

pip install matplotlib==3.2.2
import numpy as np
import matplotlib.pyplot as plt

import tensorflow as tf
import tensorflow_datasets as tfds
import tensorflow_hub as hub

उदाहरण प्रदर्शित करने के लिए सहायक कार्य

डेटासेट

आइए लोड TFDS से कसावा डाटासेट

dataset, info = tfds.load('cassava', with_info=True)

आइए इसके बारे में अधिक जानने के लिए डेटासेट जानकारी पर एक नज़र डालें, जैसे विवरण और उद्धरण और कितने उदाहरण उपलब्ध हैं, इसके बारे में जानकारी

info
tfds.core.DatasetInfo(
    name='cassava',
    full_name='cassava/0.1.0',
    description="""
    Cassava consists of leaf images for the cassava plant depicting healthy and
    four (4) disease conditions; Cassava Mosaic Disease (CMD), Cassava Bacterial
    Blight (CBB), Cassava Greem Mite (CGM) and Cassava Brown Streak Disease (CBSD).
    Dataset consists of a total of 9430 labelled images.
    The 9430 labelled images are split into a training set (5656), a test set(1885)
    and a validation set (1889). The number of images per class are unbalanced with
    the two disease classes CMD and CBSD having 72% of the images.
    """,
    homepage='https://www.kaggle.com/c/cassava-disease/overview',
    data_path='gs://tensorflow-datasets/datasets/cassava/0.1.0',
    download_size=1.26 GiB,
    dataset_size=Unknown size,
    features=FeaturesDict({
        'image': Image(shape=(None, None, 3), dtype=tf.uint8),
        'image/filename': Text(shape=(), dtype=tf.string),
        'label': ClassLabel(shape=(), dtype=tf.int64, num_classes=5),
    }),
    supervised_keys=('image', 'label'),
    disable_shuffling=False,
    splits={
        'test': <SplitInfo num_examples=1885, num_shards=4>,
        'train': <SplitInfo num_examples=5656, num_shards=8>,
        'validation': <SplitInfo num_examples=1889, num_shards=4>,
    },
    citation="""@misc{mwebaze2019icassava,
        title={iCassava 2019Fine-Grained Visual Categorization Challenge},
        author={Ernest Mwebaze and Timnit Gebru and Andrea Frome and Solomon Nsumba and Jeremy Tusubira},
        year={2019},
        eprint={1908.02900},
        archivePrefix={arXiv},
        primaryClass={cs.CV}
    }""",
)

कसावा डाटासेट 4 अलग बीमारियों के साथ कसावा पत्तियों के साथ-साथ स्वस्थ कसावा के पत्तों की छवियों है। मॉडल इन सभी वर्गों के साथ-साथ छठी कक्षा को "अज्ञात" के लिए भविष्यवाणी कर सकता है जब मॉडल अपनी भविष्यवाणी में आश्वस्त नहीं होता है।

# Extend the cassava dataset classes with 'unknown'
class_names = info.features['label'].names + ['unknown']

# Map the class names to human readable names
name_map = dict(
    cmd='Mosaic Disease',
    cbb='Bacterial Blight',
    cgm='Green Mite',
    cbsd='Brown Streak Disease',
    healthy='Healthy',
    unknown='Unknown')

print(len(class_names), 'classes:')
print(class_names)
print([name_map[name] for name in class_names])
6 classes:
['cbb', 'cbsd', 'cgm', 'cmd', 'healthy', 'unknown']
['Bacterial Blight', 'Brown Streak Disease', 'Green Mite', 'Mosaic Disease', 'Healthy', 'Unknown']

इससे पहले कि हम मॉडल को डेटा फीड कर सकें, हमें कुछ प्रीप्रोसेसिंग करने की आवश्यकता है। मॉडल [0, 1] में RGB चैनल मानों के साथ 224 x 224 छवियों की अपेक्षा करता है। आइए छवियों को सामान्य और आकार दें।

def preprocess_fn(data):
  image = data['image']

  # Normalize [0, 255] to [0, 1]
  image = tf.cast(image, tf.float32)
  image = image / 255.

  # Resize the images to 224 x 224
  image = tf.image.resize(image, (224, 224))

  data['image'] = image
  return data

आइए डेटासेट से कुछ उदाहरण देखें

batch = dataset['validation'].map(preprocess_fn).batch(25).as_numpy_iterator()
examples = next(batch)
plot(examples)

पीएनजी

नमूना

आइए टीएफ हब से क्लासिफायर को लोड करें और कुछ भविष्यवाणियां प्राप्त करें और देखें कि मॉडल की भविष्यवाणियां कुछ उदाहरणों पर हैं

classifier = hub.KerasLayer('https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2')
probabilities = classifier(examples['image'])
predictions = tf.argmax(probabilities, axis=-1)
plot(examples, predictions)

पीएनजी

मूल्यांकन और मजबूती

के डेटासेट के विभाजन पर हमारे वर्गीकारक की सटीकता मापें। हम यह भी एक गैर कसावा डेटासेट पर उसके प्रदर्शन का मूल्यांकन द्वारा मॉडल की मजबूती देख सकते हैं। INaturalist या सेम की तरह अन्य पौधे डेटासेट की छवि के लिए, मॉडल लगभग हमेशा अज्ञात लौटना चाहिए।

मापदंडों

def label_to_unknown_fn(data):
  data['label'] = 5  # Override label to unknown.
  return data
# Preprocess the examples and map the image label to unknown for non-cassava datasets.
ds = tfds.load(DATASET, split=DATASET_SPLIT).map(preprocess_fn).take(MAX_EXAMPLES)
dataset_description = DATASET
if DATASET != 'cassava':
  ds = ds.map(label_to_unknown_fn)
  dataset_description += ' (labels mapped to unknown)'
ds = ds.batch(BATCH_SIZE)

# Calculate the accuracy of the model
metric = tf.keras.metrics.Accuracy()
for examples in ds:
  probabilities = classifier(examples['image'])
  predictions = tf.math.argmax(probabilities, axis=-1)
  labels = examples['label']
  metric.update_state(labels, predictions)

print('Accuracy on %s: %.2f' % (dataset_description, metric.result().numpy()))
Accuracy on cassava: 0.88

और अधिक जानें