Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk6 min

Pneumonia Classification Using TPU in Keras: What the Example Teaches and Where It Stops

The Keras TPU example classifies chest X-rays as NORMAL or PNEUMONIA. Here is how its data pipeline, class weighting, TPU setup and metrics work, and why its test result is far below validation.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The Keras “Pneumonia Classification on TPU” example trains a convolutional neural network to label chest X-rays as NORMAL or PNEUMONIA, using TensorFlow’s TPU distribution strategy. It is a teaching example. Its own held-out test result, a binary accuracy of 0.7901, is well below the roughly 95% validation accuracy the tutorial reports, and the example does not show that the model is clinically validated or suitable for diagnosis.

What the example is built to show

The tutorial, written by Amy MiHyun Jang and first published on 2020-07-28 (last modified 2024-02-12), is aimed at Python and machine-learning learners who want a concrete pattern for running a Keras image classifier on a TPU. Its scope is narrow: one binary image task, one model, one training run. The reader takes away four things: how TFRecord files can be read and turned into labeled image tensors, how a class-imbalanced dataset is handled with class weights, how a Keras model is placed under a TPU strategy, and how precision and recall are reported alongside accuracy.

As an Amazon Associate I earn from qualifying purchases.

Running the example on a TPU

The tutorial states that it must be run in Google Colab with the TPU runtime selected. Its setup code tries to connect to a TPU and create a TensorFlow TPUStrategy; if no TPU is found, it falls back to the default strategy.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Open the tutorial on the Keras examples site, Pneumonia Classification on TPU, and launch its Colab notebook.
  2. In Colab, go to Runtime > Change runtime type.
  3. Set Hardware accelerator to TPU and select Save.
  4. Run the TPU setup cell first and read its output. Confirm that it reports a TPU connection before running training.
  5. Build the model inside the strategy scope and run training.

Step 4 matters more than it looks. The fallback path lets the notebook run on a machine without a TPU, which is useful for debugging but means the training cells will not use TPU cores. If the setup output shows the default strategy, you are not running the TPU version of the example, and timing results from that session say nothing about TPU performance.

#1 Best Overall
Google Coral USB Accelerator: ML Accelerator, USB 3.0 Type-C, Debian Linux Compatible
  • A USB accessory that brings machine learning inferencing to existing systems. Works with Raspberry Pi and other Linux systems
  • Performs high-speed ML inferencing: the on-board edge TPU Coprocessor is capable of performing 4 trillion operations (tera-operations) per second (tops), using 0.5 watts for each tops (2 tops per watt). For example, it can execute state-of-the-art mobile vision models such as mobilenet V2 AT 400 FPS, in a power efficient manner
  • Works with Debian Linux: connects to any debian-based Linux system with an included USB 3.0 Type-C cable
  • Supports tensorflow Lite: no need to build models from the ground up. Tensorflow Lite models can be compiled to run on the edge TPE
  • Supports automl vision edge: easily build and deploy fast, high-accuracy custom image classification models to your device with automl vision edge

How the data is read and prepared

The example reads ChestXRay2017 train and test splits stored as TFRecord files on Google Cloud. Each image record is zipped with a path record. The class label is taken from the directory name in the path: NORMAL maps to 0 and PNEUMONIA maps to 1. Each image is decoded as a three-channel JPEG and resized to 180 × 180 pixels.

The shuffled training dataset is split into 4,200 training examples, with the remaining examples used for validation. The validation split therefore comes from the training files, not from the separate test split, which is why validation and test numbers can diverge so sharply (see the results section).

  1. Read the image records and path records from the TFRecord files.
  2. Zip the two streams so each image is paired with its path.
  3. Derive the label from the path’s class directory (NORMAL = 0, PNEUMONIA = 1).
  4. Decode JPEG data as three channels and resize to 180 × 180.
  5. Shuffle, then take 4,200 training examples; the remainder becomes validation.

The tutorial does not explain how patients were assigned to splits, so readers should not assume that the validation set is independent at the patient level. That detail belongs to the dataset’s own documentation, which the tutorial links but this article has not verified.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Class imbalance and class weights

The training data is uneven. The tutorial counts the following images, and it uses class weights to offset the imbalance during training:

Class (label) Training images in this tutorial Class weight shown in the tutorial
NORMAL (0) 1,349 1.94
PNEUMONIA (1) 3,883 0.67

These counts describe this tutorial’s training files. They are not population rates of pneumonia, and they should not be used to estimate prevalence in any clinical setting.

The model and training setup

The model rescales pixel values from 0–255 to 0–1. It then stacks convolution and separable-convolution blocks, each followed by max pooling and batch normalization, with dropout applied to reduce overfitting. The features are flattened, passed through dense layers, and end in a single sigmoid unit that outputs the probability of PNEUMONIA.

Rank #3
M.2 Accelerator with Dual Edge TPU M.2-2230 (E-key)
  • 2x PCIe Gen2 x1 interface (one per Edge TPU)
  • M.2 - 2230 - D3 - E KEY
  • 2x Google Edge TPU ML accelerator
  • 8 TOPS total peak performance (int8)
  • 2 TOPS per watt

The model is compiled with:

  • the Adam optimizer, with exponential learning-rate decay;
  • binary cross-entropy as the loss;
  • binary accuracy, precision, and recall as metrics.

Training uses a model checkpoint and early stopping, so the saved model is not necessarily the one from the final epoch.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why precision and recall are logged

Accuracy alone can hide which kind of error a model makes. Precision shows how many of the images predicted as pneumonia really were pneumonia. Recall shows how many real pneumonia images the model caught. The tutorial reports both so that the trade-off is visible.

TPU-specific choices

Three details are specific to the TPU version:

  • Batch size is set to 25 times the number of TPU replicas in the strategy, so the global batch grows with the hardware.
  • Caching and prefetching. The tutorial caches the dataset in memory and prefetches batches so the accelerator is not left waiting for input.
  • Caching caveat. The tutorial itself warns: “Please note that large image datasets should not be cached in memory. We do it here because the dataset is not very large and we want to train on TPU.” Do not copy the in-memory cache into a pipeline built on a large image collection.

The current Keras FAQ gives the general TensorFlow pattern: connect with TPUClusterResolver, create a TPUStrategy, construct the model inside strategy.scope(), and make sure the input pipeline reads data fast enough to keep the TPU busy. The Keras FAQ on training on TPU also notes that all Keras backends (JAX, TensorFlow, PyTorch) are supported on TPU, while recommending JAX or TensorFlow for this case. The example uses TensorFlow, so it follows that recommendation.

Rank #4
Coral M.2 Accelerator A+E Key,G650-04527-01 SOM- Edge TPU ML Compute Accelerator, M.2-2230-A-E-S3
  • High-Performance ML Accelerator: Integrates Edge TPU, delivering 4 TOPS (int8) peak performance for machine learning inference tasks.
  • Strong Compatibility: Supports M.2 A+E key interface for easy integration into existing systems.
  • Low Power Design: Provides 2 TOPS per watt, ideal for embedded and energy-efficient applications.
  • Wide OS Support: Compatible with Linux (Debian 10/Ubuntu 16.04+) and Windows 10 (64-bit).
  • Industrial-Grade Reliability: Operating temperature range of -20°C to +85°C, suitable for harsh environments.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Reading the results

The tutorial’s test evaluation is the number to read first. It reports the following on the held-out test set for its own training run:

Metric (test set, tutorial run) Value What it means here
Binary accuracy 0.7901 About 79% of test images were labeled correctly.
Precision 0.7524 About 75% of images predicted as PNEUMONIA were PNEUMONIA; roughly a quarter of positive predictions were wrong.
Recall 0.9897 About 99% of the PNEUMONIA images in the test set were flagged.

The training log and the tutorial’s discussion report validation accuracy of around 95%. That gap of roughly 15 percentage points between validation and test accuracy is the most important number in the example. Keras’s own text says the lower test accuracy may indicate overfitting. It reads the higher recall than precision as the model detecting many pneumonia images while also producing false positives among NORMAL images.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

These figures come from one training run of this tutorial, on the data splits it defines. They are not a benchmark, and rerunning the notebook can produce different numbers.

Best Value
G650-04686-01 Coral M.2 Accelerator B+M Key
  • Performs high-speed ML inferencing: The on-board Edge TPU coprocessor is capable of performing 4 trillion operations (tera-operations) per second (TOPS), using 0.5 watts for each TOPS (2 TOPS per watt). For example, it can execute state-of-the-art mobile vision models such as MobileNet v2 at 400 FPS, in a power efficient manner.
  • Works with Debian Linux: Integrates with any Debian-based Linux system with a compatible card module slot.
  • Supports TensorFlow Lite: No need to build models from the ground up. TensorFlow Lite models can be compiled to run on the Edge TPU.
  • Supports AutoML Vision Edge: Easily build and deploy fast, high-accuracy custom image classification models to your device with AutoML Vision Edge.

What the example does not establish

  • Clinical validity. The tutorial is an image-classification exercise. It provides no evidence of clinical performance, radiologist comparison, or safety for medical decisions.
  • Generalization. The test gap shows the model did not transfer as well to the test images as the validation numbers suggested. The example does not test other hospitals, scanners, or patient populations.
  • Speed or accuracy versus other setups. The tutorial does not compare TPU against GPU or CPU, nor JAX or PyTorch against TensorFlow. Claims that one accelerator or backend is faster or more accurate need separate evidence.
  • Dataset construction. Patient-level splitting, representativeness, and labeling quality of the underlying chest X-ray collection are not covered in the tutorial.

How to use the example

Treat the notebook as a template for the TPU mechanics and the data pipeline: TFRecord parsing, label derivation, strategy scoping, class weighting, and metric reporting. If you adapt it, the practical changes that matter most are: split by patient rather than by image, evaluate on a separate test set that you never tune against, report precision and recall per class, and check the TPU connection before trusting any timing result.

The example is a useful piece of TensorFlow and Keras teaching code. Its own reported test numbers show that a model trained this way can look strong on validation data and still perform noticeably worse on held-out data, so any version of it that touches medical images needs clinical evaluation far beyond what this tutorial provides.

Hardware access for the tutorial is through Colab, which the tutorial requires with the TPU runtime selected. The Keras FAQ also lists Kaggle notebooks and Google Cloud Deep Learning VMs as TPU routes; availability and pricing on those services change, so check them directly before planning a run.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

Bestseller No. 1
Google Coral USB Accelerator: ML Accelerator, USB 3.0 Type-C, Debian Linux Compatible
Google Coral USB Accelerator: ML Accelerator, USB 3.0 Type-C, Debian Linux Compatible
Ml Accelerator: Google edge TPU Coprocessor; Connector: USB 3.0 Type-C (data/power); Dimensions: 65 millimeter x 30 millimeter
$135.00
Bestseller No. 3
M.2 Accelerator with Dual Edge TPU M.2-2230 (E-key)
M.2 Accelerator with Dual Edge TPU M.2-2230 (E-key)
2x PCIe Gen2 x1 interface (one per Edge TPU); M.2 - 2230 - D3 - E KEY; 2x Google Edge TPU ML accelerator
$149.47
Bestseller No. 4
Coral M.2 Accelerator A+E Key,G650-04527-01 SOM- Edge TPU ML Compute Accelerator, M.2-2230-A-E-S3
Coral M.2 Accelerator A+E Key,G650-04527-01 SOM- Edge TPU ML Compute Accelerator, M.2-2230-A-E-S3
Wide OS Support: Compatible with Linux (Debian 10/Ubuntu 16.04+) and Windows 10 (64-bit).
$89.15

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.